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

Skip to content

Commit 20c50fb

Browse files
committed
WIP
1 parent 1f8ff33 commit 20c50fb

File tree

11 files changed

+461
-151
lines changed

11 files changed

+461
-151
lines changed
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
Return value of ``pie``
2+
~~~~~~~~~~~~~~~~~~~~~~~
3+
Previously, if no *labels* were passed to `~.Axes.pie`, and *labeldistance* was
4+
not ``None``, empty text labels would be added to the axes and returned. This
5+
is no longer the case. To continue creating empty labels, either pass an empty
6+
string with the new *wedge_labels* parameter ``wedge_labels=''`` or, for
7+
compatibility with older Matplotlib versions, pass an empty string for each wedge
8+
via the *labels* parameter, i.e. ``labels=[''] * number_of_wedges``. Note the
9+
latter option will stop working at Matplotlib 3.16.
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
``pie`` *labels* and *labeldistance* parameters
2+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
3+
Currently the *labels* parameter of `~.Axes.pie` is used both for annotating the
4+
pie wedges directly, and for automatic legend entries. For consistency
5+
with other plotting methods, in future *labels* will only be used for the legend.
6+
7+
The *labeldistance* parameter will therefore default to ``None`` from Matplotlib
8+
3.14, when it will also be deprecated and then removed in Matplotlib 3.16. To
9+
preserve the existing behavior for now, set ``labeldistance=1.1``. For longer
10+
term, use the new *wedge_labels* parameter of `~.Axes.pie` instead of *labels*.

galleries/examples/misc/svg_filter_pie.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,11 +28,12 @@
2828

2929
# We want to draw the shadow for each pie, but we will not use "shadow"
3030
# option as it doesn't save the references to the shadow patches.
31-
pies = ax.pie(fracs, explode=explode, labels=labels, autopct='%1.1f%%')
31+
pies = ax.pie(fracs, explode=explode,
32+
wedge_labels=[labels, '{frac:.1%}'], wedge_label_distance=[1.1, 0.6])
3233

33-
for w in pies[0]:
34+
for w, label in zip(pies[0], labels):
3435
# set the id with the label.
35-
w.set_gid(w.get_label())
36+
w.set_gid(label)
3637

3738
# we don't want to draw the edge of the pie
3839
w.set_edgecolor("none")

galleries/examples/pie_and_polar_charts/bar_of_pie.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,9 @@
2525
explode = [0.1, 0, 0]
2626
# rotate so that first wedge is split by the x-axis
2727
angle = -180 * overall_ratios[0]
28-
wedges, *_ = ax1.pie(overall_ratios, autopct='%1.1f%%', startangle=angle,
29-
labels=labels, explode=explode)
28+
wedges, *_ = ax1.pie(
29+
overall_ratios, startangle=angle, explode=explode,
30+
wedge_labels=[labels, '{frac:.1%}'], wedge_label_distance=[1.1, 0.6])
3031

3132
# bar chart parameters
3233
age_ratios = [.33, .54, .07, .06]

galleries/examples/pie_and_polar_charts/pie_and_donut_labels.py

Lines changed: 8 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,9 @@
1515
# Now it's time for the pie. Starting with a pie recipe, we create the data
1616
# and a list of labels from it.
1717
#
18-
# We can provide a function to the ``autopct`` argument, which will expand
19-
# automatic percentage labeling by showing absolute values; we calculate
20-
# the latter back from relative data and the known sum of all values.
18+
# We can provide a format string to the *wedge_labels* parameter, to
19+
# automatically label each ingredient's wedge with its weight in grams and
20+
# percentages.
2121
#
2222
# We then create the pie and store the returned objects for later. The first
2323
# returned element of the returned tuple is a list of the wedges. Those are
@@ -31,32 +31,24 @@
3131
import matplotlib.pyplot as plt
3232
import numpy as np
3333

34-
fig, ax = plt.subplots(figsize=(6, 3), subplot_kw=dict(aspect="equal"))
34+
fig, ax = plt.subplots(figsize=(6, 3))
3535

3636
recipe = ["375 g flour",
3737
"75 g sugar",
3838
"250 g butter",
3939
"300 g berries"]
4040

41-
data = [float(x.split()[0]) for x in recipe]
41+
data = [int(x.split()[0]) for x in recipe]
4242
ingredients = [x.split()[-1] for x in recipe]
4343

44-
45-
def func(pct, allvals):
46-
absolute = int(np.round(pct/100.*np.sum(allvals)))
47-
return f"{pct:.1f}%\n({absolute:d} g)"
48-
49-
50-
wedges, texts, autotexts = ax.pie(data, autopct=lambda pct: func(pct, data),
51-
textprops=dict(color="w"))
44+
wedges, texts = ax.pie(data, wedge_labels='{frac:.1%}\n({abs:d}g)',
45+
textprops=dict(color="w", size=8, weight="bold"))
5246

5347
ax.legend(wedges, ingredients,
5448
title="Ingredients",
5549
loc="center left",
5650
bbox_to_anchor=(1, 0, 0.5, 1))
5751

58-
plt.setp(autotexts, size=8, weight="bold")
59-
6052
ax.set_title("Matplotlib bakery: A pie")
6153

6254
plt.show()
@@ -97,7 +89,7 @@ def func(pct, allvals):
9789

9890
data = [225, 90, 50, 60, 100, 5]
9991

100-
wedges, texts = ax.pie(data, wedgeprops=dict(width=0.5), startangle=-40)
92+
wedges, _ = ax.pie(data, wedgeprops=dict(width=0.5), startangle=-40)
10193

10294
bbox_props = dict(boxstyle="square,pad=0.3", fc="w", ec="k", lw=0.72)
10395
kw = dict(arrowprops=dict(arrowstyle="-"),

galleries/examples/pie_and_polar_charts/pie_features.py

Lines changed: 50 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -15,40 +15,75 @@
1515
# ------------
1616
#
1717
# Plot a pie chart of animals and label the slices. To add
18-
# labels, pass a list of labels to the *labels* parameter
18+
# labels, pass a list of labels to the *wedge_labels* parameter.
1919

2020
import matplotlib.pyplot as plt
2121

2222
labels = 'Frogs', 'Hogs', 'Dogs', 'Logs'
23-
sizes = [15, 30, 45, 10]
23+
sizes = [10, 25, 40, 5]
2424

2525
fig, ax = plt.subplots()
26-
ax.pie(sizes, labels=labels)
26+
ax.pie(sizes, wedge_labels=labels)
2727

2828
# %%
2929
# Each slice of the pie chart is a `.patches.Wedge` object; therefore in
3030
# addition to the customizations shown here, each wedge can be customized using
3131
# the *wedgeprops* argument, as demonstrated in
3232
# :doc:`/gallery/pie_and_polar_charts/nested_pie`.
3333
#
34+
# Controlling label positions
35+
# ---------------------------
36+
# If you want the labels outside the pie, set a *wedge_label_distance* greater than 1.
37+
# This is the distance from the center of the pie as a fraction of its radius.
38+
39+
fig, ax = plt.subplots()
40+
ax.pie(sizes, wedge_labels=labels, wedge_label_distance=1.1)
41+
42+
# %%
43+
#
3444
# Auto-label slices
3545
# -----------------
3646
#
37-
# Pass a function or format string to *autopct* to label slices.
47+
# Pass a format string to *wedge_labels* to label slices with their values
48+
49+
fig, ax = plt.subplots()
50+
ax.pie(sizes, wedge_labels='{abs:.1f}')
51+
52+
# %%
53+
#
54+
# or with their percentages
55+
56+
fig, ax = plt.subplots()
57+
ax.pie(sizes, wedge_labels='{frac:.1%}')
58+
59+
# %%
60+
#
61+
# or both.
3862

3963
fig, ax = plt.subplots()
40-
ax.pie(sizes, labels=labels, autopct='%1.1f%%')
64+
ax.pie(sizes, wedge_labels='{abs:d}\n{frac:.1%}')
65+
66+
67+
# %%
68+
#
69+
# Multiple labels
70+
# ---------------
71+
#
72+
# Pass both a list of labels and a format string to *wedge_labels*, with a
73+
# corresponding *wedge_label_distance* for each.
74+
75+
fig, ax = plt.subplots()
76+
ax.pie(sizes, wedge_labels=[labels, '{frac:.1%}'], wedge_label_distance=[1.1, 0.6])
4177

4278
# %%
43-
# By default, the label values are obtained from the percent size of the slice.
4479
#
4580
# Color slices
4681
# ------------
4782
#
4883
# Pass a list of colors to *colors* to set the color of each slice.
4984

5085
fig, ax = plt.subplots()
51-
ax.pie(sizes, labels=labels,
86+
ax.pie(sizes, wedge_labels=labels, wedge_label_distance=1.1,
5287
colors=['olivedrab', 'rosybrown', 'gray', 'saddlebrown'])
5388

5489
# %%
@@ -58,22 +93,10 @@
5893
# Pass a list of hatch patterns to *hatch* to set the pattern of each slice.
5994

6095
fig, ax = plt.subplots()
61-
ax.pie(sizes, labels=labels, hatch=['**O', 'oO', 'O.O', '.||.'])
62-
63-
# %%
64-
# Swap label and autopct text positions
65-
# -------------------------------------
66-
# Use the *labeldistance* and *pctdistance* parameters to position the *labels*
67-
# and *autopct* text respectively.
68-
69-
fig, ax = plt.subplots()
70-
ax.pie(sizes, labels=labels, autopct='%1.1f%%',
71-
pctdistance=1.25, labeldistance=.6)
96+
ax.pie(sizes, wedge_labels=labels, wedge_label_distance=1.1,
97+
hatch=['**O', 'oO', 'O.O', '.||.'])
7298

7399
# %%
74-
# *labeldistance* and *pctdistance* are ratios of the radius; therefore they
75-
# vary between ``0`` for the center of the pie and ``1`` for the edge of the
76-
# pie, and can be set to greater than ``1`` to place text outside the pie.
77100
#
78101
# Explode, shade, and rotate slices
79102
# ---------------------------------
@@ -89,8 +112,8 @@
89112
explode = (0, 0.1, 0, 0) # only "explode" the 2nd slice (i.e. 'Hogs')
90113

91114
fig, ax = plt.subplots()
92-
ax.pie(sizes, explode=explode, labels=labels, autopct='%1.1f%%',
93-
shadow=True, startangle=90)
115+
ax.pie(sizes, explode=explode, wedge_labels=[labels, '{frac:.1%}'],
116+
wedge_label_distance=[1.1, 0.6], shadow=True, startangle=90)
94117
plt.show()
95118

96119
# %%
@@ -107,7 +130,8 @@
107130

108131
fig, ax = plt.subplots()
109132

110-
ax.pie(sizes, labels=labels, autopct='%.0f%%',
133+
ax.pie(sizes, wedge_labels=[labels, '{frac:.1%}'],
134+
wedge_label_distance=[1.1, 0.6],
111135
textprops={'size': 'small'}, radius=0.5)
112136
plt.show()
113137

@@ -119,7 +143,8 @@
119143
# the `.Shadow` patch. This can be used to modify the default shadow.
120144

121145
fig, ax = plt.subplots()
122-
ax.pie(sizes, explode=explode, labels=labels, autopct='%1.1f%%',
146+
ax.pie(sizes, explode=explode, wedge_labels=[labels, '{frac:.1%}'],
147+
wedge_label_distance=[1.1, 0.6],
123148
shadow={'ox': -0.04, 'edgecolor': 'none', 'shade': 0.9}, startangle=90)
124149
plt.show()
125150

lib/matplotlib/__init__.py

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,7 @@
137137

138138
import atexit
139139
from collections import namedtuple
140-
from collections.abc import MutableMapping
140+
from collections.abc import MutableMapping, Sequence
141141
import contextlib
142142
import functools
143143
import importlib
@@ -1362,14 +1362,17 @@ def _init_tests():
13621362

13631363
def _replacer(data, value):
13641364
"""
1365-
Either returns ``data[value]`` or passes ``data`` back, converts either to
1366-
a sequence.
1365+
Either returns ``data[value]`` or passes ``value`` back, converts either to
1366+
a sequence. If ``value`` is a non-string sequence, processes each element
1367+
and returns a list.
13671368
"""
13681369
try:
1369-
# if key isn't a string don't bother
1370+
# if key isn't a string or sequence don't bother
13701371
if isinstance(value, str):
13711372
# try to use __getitem__
13721373
value = data[value]
1374+
elif isinstance(value, Sequence):
1375+
return [_replacer(data, x) for x in value]
13731376
except Exception:
13741377
# key does not exist, silently fall back to key
13751378
pass
@@ -1444,7 +1447,9 @@ def func(ax, *args, **kwargs): ...
14441447
- if called with ``data=None``, forward the other arguments to ``func``;
14451448
- otherwise, *data* must be a mapping; for any argument passed in as a
14461449
string ``name``, replace the argument by ``data[name]`` (if this does not
1447-
throw an exception), then forward the arguments to ``func``.
1450+
throw an exception), then forward the arguments to ``func``. For any
1451+
argument passed as a non-string sequence, replace any string elements
1452+
by ``data[name]`` (if that does not throw an exception).
14481453
14491454
In either case, any argument that is a `MappingView` is also converted to a
14501455
list.

0 commit comments

Comments
 (0)