Skip to content

Commit a39e43c

Browse files
committed
Add configuration of Shadow and pie shadow
1 parent 5a34696 commit a39e43c

File tree

4 files changed

+32
-7
lines changed

4 files changed

+32
-7
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
The pie chart shadow can be controlled
2+
--------------------------------------
3+
4+
The *shadow* argument to `~.Axes.pie` can now be a dict, allowing more control
5+
of the `.Shadow`-patch used.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
Shadow shade can be controlled
2+
------------------------------
3+
4+
The `.Shadow` patch now has a *shade* argument to control the shadow darkness.
5+
If 0, the shadow is black, if 1, the shadow has the same color as the patch that
6+
is shadowed. The default value, which earlier was fixed, is 0.3.

lib/matplotlib/axes/_axes.py

+10-4
Original file line numberDiff line numberDiff line change
@@ -3102,8 +3102,12 @@ def pie(self, x, explode=None, labels=None, colors=None,
31023102
If set to ``None``, labels are not drawn but are still stored for
31033103
use in `.legend`.
31043104
3105-
shadow : bool, default: False
3106-
Draw a shadow beneath the pie.
3105+
shadow : bool or dict, default: False
3106+
If bool, whether to draw a shadow beneath the pie. If dict, draw a shadow
3107+
passing the properties in the dict to `.Shadow`.
3108+
3109+
.. versionadded:: 3.8
3110+
*shadow* can be a dict.
31073111
31083112
startangle : float, default: 0 degrees
31093113
The angle by which the start of the pie is rotated,
@@ -3231,8 +3235,10 @@ def get_next_color():
32313235
if shadow:
32323236
# Make sure to add a shadow after the call to add_patch so the
32333237
# figure and transform props will be set.
3234-
shad = mpatches.Shadow(w, -0.02, -0.02, label='_nolegend_')
3235-
self.add_patch(shad)
3238+
shadow_dict = {'ox': -0.02, 'oy': -0.02, 'label': '_nolegend_'}
3239+
if isinstance(shadow, dict):
3240+
shadow_dict.update(shadow)
3241+
self.add_patch(mpatches.Shadow(w, **shadow_dict))
32363242

32373243
if labeldistance is not None:
32383244
xt = x + labeldistance * radius * math.cos(thetam)

lib/matplotlib/patches.py

+11-3
Original file line numberDiff line numberDiff line change
@@ -614,12 +614,12 @@ def __str__(self):
614614
return f"Shadow({self.patch})"
615615

616616
@_docstring.dedent_interpd
617-
def __init__(self, patch, ox, oy, **kwargs):
617+
def __init__(self, patch, ox, oy, *, shade=0.3, **kwargs):
618618
"""
619619
Create a shadow of the given *patch*.
620620
621621
By default, the shadow will have the same face color as the *patch*,
622-
but darkened.
622+
but darkened. The darkness can be controlled by *shade*.
623623
624624
Parameters
625625
----------
@@ -628,6 +628,12 @@ def __init__(self, patch, ox, oy, **kwargs):
628628
ox, oy : float
629629
The shift of the shadow in data coordinates, scaled by a factor
630630
of dpi/72.
631+
shade : float, default: 0.3
632+
How the darkness of the shadow relates to the original color. If 0, the
633+
shadow is black, if 1, the shadow has the same color as the *patch*.
634+
635+
.. versionadded:: 3.8
636+
631637
**kwargs
632638
Properties of the shadow patch. Supported keys are:
633639
@@ -639,7 +645,9 @@ def __init__(self, patch, ox, oy, **kwargs):
639645
self._shadow_transform = transforms.Affine2D()
640646

641647
self.update_from(self.patch)
642-
color = .3 * np.asarray(colors.to_rgb(self.patch.get_facecolor()))
648+
if not 0 <= shade <= 1:
649+
raise ValueError("shade must be between 0 and 1.")
650+
color = shade * np.asarray(colors.to_rgb(self.patch.get_facecolor()))
643651
self.update({'facecolor': color, 'edgecolor': color, 'alpha': 0.5,
644652
# Place shadow patch directly behind the inherited patch.
645653
'zorder': np.nextafter(self.patch.zorder, -np.inf),

0 commit comments

Comments
 (0)