Skip to content

Commit 2e7b3c6

Browse files
committed
[ENH] Vectorized Wedgeprops argument to pie
1 parent 5093150 commit 2e7b3c6

File tree

2 files changed

+28
-2
lines changed

2 files changed

+28
-2
lines changed

lib/matplotlib/axes/_axes.py

+10-2
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import collections
12
import functools
23
import itertools
34
import logging
@@ -3226,7 +3227,14 @@ def get_next_color():
32263227
slices = []
32273228
autotexts = []
32283229

3229-
for frac, label, expl in zip(x, labels, explode):
3230+
if isinstance(wedgeprops, collections.abc.Sequence):
3231+
if len(wedgeprops) != len(x):
3232+
raise ValueError(f'wedgeprops length={len(wedgeprops)}'
3233+
f' and input length={len(x)} are not equal')
3234+
else:
3235+
wedgeprops = itertools.repeat(wedgeprops)
3236+
3237+
for frac, label, expl, wprops in zip(x, labels, explode, wedgeprops):
32303238
x, y = center
32313239
theta2 = (theta1 + frac) if counterclock else (theta1 - frac)
32323240
thetam = 2 * np.pi * 0.5 * (theta1 + theta2)
@@ -3238,7 +3246,7 @@ def get_next_color():
32383246
facecolor=get_next_color(),
32393247
clip_on=False,
32403248
label=label)
3241-
w.set(**wedgeprops)
3249+
w.set(**wprops)
32423250
slices.append(w)
32433251
self.add_patch(w)
32443252

lib/matplotlib/tests/test_axes.py

+18
Original file line numberDiff line numberDiff line change
@@ -5740,6 +5740,24 @@ def test_normalize_kwarg_pie():
57405740
assert abs(t2[0][-1].theta2 - 360.) > 1e-3
57415741

57425742

5743+
def test_pie_wedgeprops_error():
5744+
fig, ax = plt.subplots()
5745+
x = [0.3, 0.3, 0.1]
5746+
wedgeprops = [{'a': 1}]
5747+
with pytest.raises(ValueError, match=r"length=1 .* length=3"):
5748+
ax.pie(x, wedgeprops=wedgeprops)
5749+
5750+
5751+
@check_figures_equal()
5752+
def test_pie_multi_wedgeprops(fig_test, fig_ref):
5753+
x = [0.3, 0.3, 0.1]
5754+
wedgeprops = [{'hatch': '/'}, {'hatch': '+'}, {'hatch': '.'}]
5755+
fig_test.subplots().pie(x, wedgeprops=wedgeprops)
5756+
wedges, _ = fig_ref.subplots().pie(x)
5757+
for w, wp in zip(wedges, wedgeprops):
5758+
w.set(**wp)
5759+
5760+
57435761
@image_comparison(['set_get_ticklabels.png'])
57445762
def test_set_get_ticklabels():
57455763
# test issue 2246

0 commit comments

Comments
 (0)