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

Skip to content

Commit acfcd48

Browse files
committed
[ENH] Vectorized Wedgeprops argument to pie
1 parent bcdd07a commit acfcd48

File tree

2 files changed

+34
-7
lines changed

2 files changed

+34
-7
lines changed

lib/matplotlib/axes/_axes.py

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import collections
12
import functools
23
import itertools
34
import logging
@@ -3116,11 +3117,12 @@ def pie(self, x, explode=None, labels=None, colors=None,
31163117
counterclock : bool, default: True
31173118
Specify fractions direction, clockwise or counterclockwise.
31183119
3119-
wedgeprops : dict, default: None
3120-
Dict of arguments passed to the wedge objects making the pie.
3121-
For example, you can pass in ``wedgeprops = {'linewidth': 3}``
3122-
to set the width of the wedge border lines equal to 3.
3123-
For more details, look at the doc/arguments of the wedge object.
3120+
wedgeprops : dict, list of dict, default: None
3121+
Dict of arguments passed to each `matplotlib.patches.Wedge` of the
3122+
pie. For example, you can pass in ``wedgeprops = {'linewidth': 3}``
3123+
to set the width of the wedge border lines equal to 3. You can also
3124+
pass in a list of dicts to set each individual wedge object.
3125+
For a list of properties, see `matplotlib.patches.Wedge`.
31243126
By default ``clip_on=False``.
31253127
31263128
textprops : dict, default: None
@@ -3205,7 +3207,14 @@ def get_next_color():
32053207
slices = []
32063208
autotexts = []
32073209

3208-
for frac, label, expl in zip(x, labels, explode):
3210+
if isinstance(wedgeprops, collections.abc.Sequence):
3211+
if len(wedgeprops) != len(x):
3212+
raise ValueError(f'wedgeprops length={len(wedgeprops)}'
3213+
f' and input length={len(x)} are not equal')
3214+
else:
3215+
wedgeprops = itertools.repeat(wedgeprops)
3216+
3217+
for frac, label, expl, wprops in zip(x, labels, explode, wedgeprops):
32093218
x, y = center
32103219
theta2 = (theta1 + frac) if counterclock else (theta1 - frac)
32113220
thetam = 2 * np.pi * 0.5 * (theta1 + theta2)
@@ -3217,7 +3226,7 @@ def get_next_color():
32173226
facecolor=get_next_color(),
32183227
clip_on=False,
32193228
label=label)
3220-
w.set(**wedgeprops)
3229+
w.set(**wprops)
32213230
slices.append(w)
32223231
self.add_patch(w)
32233232

lib/matplotlib/tests/test_axes.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5709,6 +5709,24 @@ def test_normalize_kwarg_pie():
57095709
assert abs(t2[0][-1].theta2 - 360.) > 1e-3
57105710

57115711

5712+
def test_pie_wedgeprops_error():
5713+
fig, ax = plt.subplots()
5714+
x = [0.3, 0.3, 0.1]
5715+
wedgeprops = [{'a': 1}]
5716+
with pytest.raises(ValueError, match=r"length=1 .* length=3"):
5717+
ax.pie(x, wedgeprops=wedgeprops)
5718+
5719+
5720+
@check_figures_equal()
5721+
def test_pie_multi_wedgeprops(fig_test, fig_ref):
5722+
x = [0.3, 0.3, 0.1]
5723+
wedgeprops = [{'hatch': '/'}, {'hatch': '+'}, {'hatch': '.'}]
5724+
fig_test.subplots().pie(x, wedgeprops=wedgeprops)
5725+
wedges, _ = fig_ref.subplots().pie(x)
5726+
for w, wp in zip(wedges, wedgeprops):
5727+
w.set(**wp)
5728+
5729+
57125730
@image_comparison(['set_get_ticklabels.png'])
57135731
def test_set_get_ticklabels():
57145732
# test issue 2246

0 commit comments

Comments
 (0)