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

Skip to content

Commit 425320f

Browse files
authored
Merge pull request #24937 from meeseeksmachine/auto-backport-of-pr-24470-on-v3.7.x
Backport PR #24470 on branch v3.7.x ([ENH] hatch keyword for pie + some pie documentation)
2 parents 1fb4d50 + 578758a commit 425320f

File tree

4 files changed

+73
-24
lines changed

4 files changed

+73
-24
lines changed
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
``hatch`` parameter for pie
2+
-------------------------------------------
3+
4+
`~matplotlib.axes.Axes.pie` now accepts a *hatch* keyword that takes as input
5+
a hatch or list of hatches:
6+
7+
.. plot::
8+
:include-source: true
9+
:alt: Two pie charts, identified as ax1 and ax2, both have a small blue slice, a medium orange slice, and a large green slice. ax1 has a dot hatching on the small slice, a small open circle hatching on the medium slice, and a large open circle hatching on the large slice. ax2 has the same large open circle with a dot hatch on every slice.
10+
11+
fig, (ax1, ax2) = plt.subplots(ncols=2)
12+
x = [10, 30, 60]
13+
14+
ax1.pie(x, hatch=['.', 'o', 'O'])
15+
ax2.pie(x, hatch='.O')
16+
17+
ax1.set_title("hatch=['.', 'o', 'O']")
18+
ax2.set_title("hatch='.O'")

lib/matplotlib/axes/_axes.py

Lines changed: 35 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -3075,7 +3075,7 @@ 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):
3078+
frame=False, rotatelabels=False, *, normalize=True, hatch=None):
30793079
"""
30803080
Plot a pie chart.
30813081
@@ -3101,29 +3101,34 @@ def pie(self, x, explode=None, labels=None, colors=None,
31013101
A sequence of colors through which the pie chart will cycle. If
31023102
*None*, will use the colors in the currently active cycle.
31033103
3104+
hatch : str or list, default: None
3105+
Hatching pattern applied to all pie wedges or sequence of patterns
3106+
through which the chart will cycle. For a list of valid patterns,
3107+
see :doc:`/gallery/shapes_and_collections/hatch_style_reference`.
3108+
3109+
.. versionadded:: 3.7
3110+
31043111
autopct : None or str or callable, default: None
3105-
If not *None*, is a string or function used to label the wedges
3106-
with their numeric value. The label will be placed inside the
3107-
wedge. If it is a format string, the label will be ``fmt % pct``.
3108-
If it is a function, it will be called.
3112+
If not *None*, *autopct* is a string or function used to label the
3113+
wedges with their numeric value. The label will be placed inside
3114+
the wedge. If *autopct* is a format string, the label will be
3115+
``fmt % pct``. If *autopct* is a function, then it will be called.
31093116
31103117
pctdistance : float, default: 0.6
3111-
The ratio between the center of each pie slice and the start of
3112-
the text generated by *autopct*. Ignored if *autopct* is *None*.
3118+
The relative distance along the radius at which the the text
3119+
generated by *autopct* is drawn. To draw the text outside the pie,
3120+
set *pctdistance* > 1. This parameter is ignored if *autopct* is
3121+
``None``.
3122+
3123+
labeldistance : float or None, default: 1.1
3124+
The relative distance along the radius at which the labels are
3125+
drawn. To draw the labels inside the pie, set *labeldistance* < 1.
3126+
If set to ``None``, labels are not drawn but are still stored for
3127+
use in `.legend`.
31133128
31143129
shadow : bool, default: False
31153130
Draw a shadow beneath the pie.
31163131
3117-
normalize : bool, default: True
3118-
When *True*, always make a full pie by normalizing x so that
3119-
``sum(x) == 1``. *False* makes a partial pie if ``sum(x) <= 1``
3120-
and raises a `ValueError` for ``sum(x) > 1``.
3121-
3122-
labeldistance : float or None, default: 1.1
3123-
The radial distance at which the pie labels are drawn.
3124-
If set to ``None``, label are not drawn, but are stored for use in
3125-
``legend()``
3126-
31273132
startangle : float, default: 0 degrees
31283133
The angle by which the start of the pie is rotated,
31293134
counterclockwise from the x-axis.
@@ -3135,11 +3140,11 @@ def pie(self, x, explode=None, labels=None, colors=None,
31353140
Specify fractions direction, clockwise or counterclockwise.
31363141
31373142
wedgeprops : dict, default: None
3138-
Dict of arguments passed to the wedge objects making the pie.
3139-
For example, you can pass in ``wedgeprops = {'linewidth': 3}``
3140-
to set the width of the wedge border lines equal to 3.
3141-
For more details, look at the doc/arguments of the wedge object.
3142-
By default, ``clip_on=False``.
3143+
Dict of arguments passed to each `.patches.Wedge` of the pie.
3144+
For example, ``wedgeprops = {'linewidth': 3}`` sets the width of
3145+
the wedge border lines equal to 3. By default, ``clip_on=False``.
3146+
When there is a conflict between these properties and other
3147+
keywords, properties passed to *wedgeprops* take precedence.
31433148
31443149
textprops : dict, default: None
31453150
Dict of arguments to pass to the text objects.
@@ -3153,6 +3158,11 @@ def pie(self, x, explode=None, labels=None, colors=None,
31533158
rotatelabels : bool, default: False
31543159
Rotate each label to the angle of the corresponding slice if true.
31553160
3161+
normalize : bool, default: True
3162+
When *True*, always make a full pie by normalizing x so that
3163+
``sum(x) == 1``. *False* makes a partial pie if ``sum(x) <= 1``
3164+
and raises a `ValueError` for ``sum(x) > 1``.
3165+
31563166
data : indexable object, optional
31573167
DATA_PARAMETER_PLACEHOLDER
31583168
@@ -3207,6 +3217,8 @@ def pie(self, x, explode=None, labels=None, colors=None,
32073217
def get_next_color():
32083218
return next(color_cycle)
32093219

3220+
hatch_cycle = itertools.cycle(np.atleast_1d(hatch))
3221+
32103222
_api.check_isinstance(Number, radius=radius, startangle=startangle)
32113223
if radius <= 0:
32123224
raise ValueError(f'radius must be a positive number, not {radius}')
@@ -3233,6 +3245,7 @@ def get_next_color():
32333245
w = mpatches.Wedge((x, y), radius, 360. * min(theta1, theta2),
32343246
360. * max(theta1, theta2),
32353247
facecolor=get_next_color(),
3248+
hatch=next(hatch_cycle),
32363249
clip_on=False,
32373250
label=label)
32383251
w.set(**wedgeprops)

lib/matplotlib/pyplot.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2779,15 +2779,15 @@ def pie(
27792779
pctdistance=0.6, shadow=False, labeldistance=1.1,
27802780
startangle=0, radius=1, counterclock=True, wedgeprops=None,
27812781
textprops=None, center=(0, 0), frame=False,
2782-
rotatelabels=False, *, normalize=True, data=None):
2782+
rotatelabels=False, *, normalize=True, hatch=None, data=None):
27832783
return gca().pie(
27842784
x, explode=explode, labels=labels, colors=colors,
27852785
autopct=autopct, pctdistance=pctdistance, shadow=shadow,
27862786
labeldistance=labeldistance, startangle=startangle,
27872787
radius=radius, counterclock=counterclock,
27882788
wedgeprops=wedgeprops, textprops=textprops, center=center,
27892789
frame=frame, rotatelabels=rotatelabels, normalize=normalize,
2790-
**({"data": data} if data is not None else {}))
2790+
hatch=hatch, **({"data": data} if data is not None else {}))
27912791

27922792

27932793
# Autogenerated by boilerplate.py. Do not edit as changes will be lost.

lib/matplotlib/tests/test_axes.py

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

57465746

5747+
@check_figures_equal()
5748+
def test_pie_hatch_single(fig_test, fig_ref):
5749+
x = [0.3, 0.3, 0.1]
5750+
hatch = '+'
5751+
fig_test.subplots().pie(x, hatch=hatch)
5752+
wedges, _ = fig_ref.subplots().pie(x)
5753+
[w.set_hatch(hatch) for w in wedges]
5754+
5755+
5756+
@check_figures_equal()
5757+
def test_pie_hatch_multi(fig_test, fig_ref):
5758+
x = [0.3, 0.3, 0.1]
5759+
hatch = ['/', '+', '.']
5760+
fig_test.subplots().pie(x, hatch=hatch)
5761+
wedges, _ = fig_ref.subplots().pie(x)
5762+
[w.set_hatch(hp) for w, hp in zip(wedges, hatch)]
5763+
5764+
57475765
@image_comparison(['set_get_ticklabels.png'])
57485766
def test_set_get_ticklabels():
57495767
# test issue 2246

0 commit comments

Comments
 (0)