Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 24e26eb

Browse files
committed
Add configuration of Shadow and pie shadow
1 parent 7c07acf commit 24e26eb

File tree

2 files changed

+22
-5
lines changed

2 files changed

+22
-5
lines changed

lib/matplotlib/axes/_axes.py

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3075,7 +3075,8 @@ def pie(self, x, explode=None, labels=None, colors=None,
30753075
autopct=None, pctdistance=0.6, shadow=False, labeldistance=1.1,
30763076
startangle=0, radius=1, counterclock=True,
30773077
wedgeprops=None, textprops=None, center=(0, 0),
3078-
frame=False, rotatelabels=False, *, normalize=True, hatch=None):
3078+
frame=False, rotatelabels=False, *, normalize=True, hatch=None,
3079+
shadow_factor=0.3, shadow_offset=(-0.02, -0.02), shadow_alpha=0.5):
30793080
"""
30803081
Plot a pie chart.
30813082
@@ -3163,6 +3164,16 @@ def pie(self, x, explode=None, labels=None, colors=None,
31633164
``sum(x) == 1``. *False* makes a partial pie if ``sum(x) <= 1``
31643165
and raises a `ValueError` for ``sum(x) > 1``.
31653166
3167+
shadow_factor : float, default: 0.3
3168+
How the darkness of the shadow relates to the original color. If 0, the
3169+
shadow is black, if 1, the shadow has the same color as the *patch*.
3170+
3171+
shadow_offset : (float, float), default: (-0.02, -0.02)
3172+
The (x, y)-offset of the shadow.
3173+
3174+
shadow_alpha : float, default: 0.5
3175+
The alpha value of the shadow.
3176+
31663177
data : indexable object, optional
31673178
DATA_PARAMETER_PLACEHOLDER
31683179
@@ -3255,7 +3266,8 @@ def get_next_color():
32553266
if shadow:
32563267
# Make sure to add a shadow after the call to add_patch so the
32573268
# figure and transform props will be set.
3258-
shad = mpatches.Shadow(w, -0.02, -0.02, label='_nolegend_')
3269+
shad = mpatches.Shadow(w, *shadow_offset, shadow_factor=shadow_factor,
3270+
alpha=shadow_alpha, label='_nolegend_')
32593271
self.add_patch(shad)
32603272

32613273
if labeldistance is not None:

lib/matplotlib/patches.py

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -615,12 +615,12 @@ def __str__(self):
615615
return f"Shadow({self.patch})"
616616

617617
@_docstring.dedent_interpd
618-
def __init__(self, patch, ox, oy, **kwargs):
618+
def __init__(self, patch, ox, oy, *, shadow_factor=0.3, **kwargs):
619619
"""
620620
Create a shadow of the given *patch*.
621621
622622
By default, the shadow will have the same face color as the *patch*,
623-
but darkened.
623+
but darkened. The darkness can be controlled by *shadow_factor*.
624624
625625
Parameters
626626
----------
@@ -629,6 +629,9 @@ def __init__(self, patch, ox, oy, **kwargs):
629629
ox, oy : float
630630
The shift of the shadow in data coordinates, scaled by a factor
631631
of dpi/72.
632+
shadow_factor : float, default: 0.3
633+
How the darkness of the shadow relates to the original color. If 0, the
634+
shadow is black, if 1, the shadow has the same color as the *patch*.
632635
**kwargs
633636
Properties of the shadow patch. Supported keys are:
634637
@@ -640,7 +643,9 @@ def __init__(self, patch, ox, oy, **kwargs):
640643
self._shadow_transform = transforms.Affine2D()
641644

642645
self.update_from(self.patch)
643-
color = .3 * np.asarray(colors.to_rgb(self.patch.get_facecolor()))
646+
if not 0 <= shadow_factor <= 1:
647+
raise ValueError("shadow_factor must be between 0 and 1.")
648+
color = shadow_factor * np.asarray(colors.to_rgb(self.patch.get_facecolor()))
644649
self.update({'facecolor': color, 'edgecolor': color, 'alpha': 0.5,
645650
# Place shadow patch directly behind the inherited patch.
646651
'zorder': np.nextafter(self.patch.zorder, -np.inf),

0 commit comments

Comments
 (0)