Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 7b7c8fa

Browse filesBrowse files
story645oscargussledziu32
committed
added hatch kwarg arg + tests + what's new
reworded pctdistance and labeldistance b/c of #24789 Co-authored-by: Oscar Gustafsson <oscar.gustafsson@gmail.com> Co-authored-by: sledziu32 <sledziu32@gmail.com>
1 parent b62376c commit 7b7c8fa
Copy full SHA for 7b7c8fa

File tree

4 files changed

+70
-25
lines changed
Filter options

4 files changed

+70
-25
lines changed
+14Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
``hatch`` *kwarg* 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+
10+
fig, (ax1, ax2) = plt.subplots(ncols=2)
11+
w1,_ = ax1.pie([1,1,1,1], hatch='\/|-', colors=['whitesmoke','silver'],
12+
wedgeprops={'edgecolor':'dimgray'})
13+
w2,_ = ax2.pie([1,1,1,1], hatch=[ '-O', '-', '-.O', '-.'],
14+
colors=['thistle'], wedgeprops={'edgecolor':'darkmagenta'})

‎lib/matplotlib/axes/_axes.py

Copy file name to clipboardExpand all lines: lib/matplotlib/axes/_axes.py
+36-23Lines changed: 36 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -3083,7 +3083,7 @@ def pie(self, x, explode=None, labels=None, colors=None,
30833083
autopct=None, pctdistance=0.6, shadow=False, labeldistance=1.1,
30843084
startangle=0, radius=1, counterclock=True,
30853085
wedgeprops=None, textprops=None, center=(0, 0),
3086-
frame=False, rotatelabels=False, *, normalize=True):
3086+
frame=False, rotatelabels=False, *, normalize=True, hatch=None):
30873087
"""
30883088
Plot a pie chart.
30893089
@@ -3109,29 +3109,34 @@ def pie(self, x, explode=None, labels=None, colors=None,
31093109
A sequence of colors through which the pie chart will cycle. If
31103110
*None*, will use the colors in the currently active cycle.
31113111
3112+
hatch : str or list, default: None
3113+
Hatching pattern applied to all pie wedges or sequence of patterns
3114+
through which the chart will cycle. For a list of valid patterns,
3115+
see :doc:`/gallery/shapes_and_collections/hatch_style_reference`
3116+
3117+
.. versionadded:: 3.7
3118+
31123119
autopct : None or str or callable, default: None
3113-
If not *None*, is a string or function used to label the wedges
3114-
with their numeric value. The label will be placed inside the
3115-
wedge. If it is a format string, the label will be ``fmt % pct``.
3116-
If it is a function, it will be called.
3120+
If not *None*, *autopct* a string or function used to label the
3121+
wedges with their numeric value. The label will be placed inside
3122+
the wedge. If it is a format string, the label will be
3123+
``fmt % pct``. If it is a function, it will be called.
31173124
31183125
pctdistance : float, default: 0.6
3119-
The ratio between the center of each pie slice and the start of
3120-
the text generated by *autopct*. Ignored if *autopct* is *None*.
3126+
The relative distance along the radius at which the the text
3127+
generated by *autopct* is drawn. To draw the text outside the pie,
3128+
set *pctdistance* > 1. This parameter is ignored if *autopct* is
3129+
``None``.
3130+
3131+
labeldistance : float or None, default: 1.1
3132+
The relative distance along the radius at which the labels are
3133+
drawn. To draw the labels inside the pie, set *labeldistance* < 1.
3134+
If set to ``None``, labels are not drawn but are still stored for
3135+
use in `.legend`.
31213136
31223137
shadow : bool, default: False
31233138
Draw a shadow beneath the pie.
31243139
3125-
normalize : bool, default: True
3126-
When *True*, always make a full pie by normalizing x so that
3127-
``sum(x) == 1``. *False* makes a partial pie if ``sum(x) <= 1``
3128-
and raises a `ValueError` for ``sum(x) > 1``.
3129-
3130-
labeldistance : float or None, default: 1.1
3131-
The radial distance at which the pie labels are drawn.
3132-
If set to ``None``, label are not drawn, but are stored for use in
3133-
``legend()``
3134-
31353140
startangle : float, default: 0 degrees
31363141
The angle by which the start of the pie is rotated,
31373142
counterclockwise from the x-axis.
@@ -3143,11 +3148,11 @@ def pie(self, x, explode=None, labels=None, colors=None,
31433148
Specify fractions direction, clockwise or counterclockwise.
31443149
31453150
wedgeprops : dict, default: None
3146-
Dict of arguments passed to the wedge objects making the pie.
3147-
For example, you can pass in ``wedgeprops = {'linewidth': 3}``
3148-
to set the width of the wedge border lines equal to 3.
3149-
For more details, look at the doc/arguments of the wedge object.
3150-
By default, ``clip_on=False``.
3151+
Dict of arguments passed to each `.patches.Wedge` of the pie.
3152+
For example, ``wedgeprops = {'linewidth': 3}`` sets the width of
3153+
the wedge border lines equal to 3. By default, ``clip_on=False``.
3154+
When there is a conflict between these properties and other
3155+
keywords, properties passed to *wedgeprops* take precedence.
31513156
31523157
textprops : dict, default: None
31533158
Dict of arguments to pass to the text objects.
@@ -3161,6 +3166,11 @@ def pie(self, x, explode=None, labels=None, colors=None,
31613166
rotatelabels : bool, default: False
31623167
Rotate each label to the angle of the corresponding slice if true.
31633168
3169+
normalize : bool, default: True
3170+
When *True*, always make a full pie by normalizing x so that
3171+
``sum(x) == 1``. *False* makes a partial pie if ``sum(x) <= 1``
3172+
and raises a `ValueError` for ``sum(x) > 1``.
3173+
31643174
data : indexable object, optional
31653175
DATA_PARAMETER_PLACEHOLDER
31663176
@@ -3231,7 +3241,9 @@ def get_next_color():
32313241
slices = []
32323242
autotexts = []
32333243

3234-
for frac, label, expl in zip(x, labels, explode):
3244+
hatch = itertools.cycle(np.atleast_1d(hatch))
3245+
3246+
for frac, label, expl, htch in zip(x, labels, explode, hatch):
32353247
x, y = center
32363248
theta2 = (theta1 + frac) if counterclock else (theta1 - frac)
32373249
thetam = 2 * np.pi * 0.5 * (theta1 + theta2)
@@ -3241,6 +3253,7 @@ def get_next_color():
32413253
w = mpatches.Wedge((x, y), radius, 360. * min(theta1, theta2),
32423254
360. * max(theta1, theta2),
32433255
facecolor=get_next_color(),
3256+
hatch=htch,
32443257
clip_on=False,
32453258
label=label)
32463259
w.set(**wedgeprops)

‎lib/matplotlib/pyplot.py

Copy file name to clipboardExpand all lines: lib/matplotlib/pyplot.py
+2-2Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2755,15 +2755,15 @@ def pie(
27552755
pctdistance=0.6, shadow=False, labeldistance=1.1,
27562756
startangle=0, radius=1, counterclock=True, wedgeprops=None,
27572757
textprops=None, center=(0, 0), frame=False,
2758-
rotatelabels=False, *, normalize=True, data=None):
2758+
rotatelabels=False, *, normalize=True, hatch=None, data=None):
27592759
return gca().pie(
27602760
x, explode=explode, labels=labels, colors=colors,
27612761
autopct=autopct, pctdistance=pctdistance, shadow=shadow,
27622762
labeldistance=labeldistance, startangle=startangle,
27632763
radius=radius, counterclock=counterclock,
27642764
wedgeprops=wedgeprops, textprops=textprops, center=center,
27652765
frame=frame, rotatelabels=rotatelabels, normalize=normalize,
2766-
**({"data": data} if data is not None else {}))
2766+
hatch=hatch, **({"data": data} if data is not None else {}))
27672767

27682768

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

‎lib/matplotlib/tests/test_axes.py

Copy file name to clipboardExpand all lines: lib/matplotlib/tests/test_axes.py
+18Lines changed: 18 additions & 0 deletions
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+
@check_figures_equal()
5744+
def test_pie_hatch_single(fig_test, fig_ref):
5745+
x = [0.3, 0.3, 0.1]
5746+
hatch = '+'
5747+
fig_test.subplots().pie(x, hatch=hatch)
5748+
wedges, _ = fig_ref.subplots().pie(x)
5749+
[w.set_hatch(hatch) for w in wedges]
5750+
5751+
5752+
@check_figures_equal()
5753+
def test_pie_hatch_multi(fig_test, fig_ref):
5754+
x = [0.3, 0.3, 0.1]
5755+
hatch = ['/', '+', '.']
5756+
fig_test.subplots().pie(x, hatch=hatch)
5757+
wedges, _ = fig_ref.subplots().pie(x)
5758+
[w.set_hatch(hp) for w, hp in zip(wedges, hatch)]
5759+
5760+
57435761
@image_comparison(['set_get_ticklabels.png'])
57445762
def test_set_get_ticklabels():
57455763
# test issue 2246

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.