|
6 | 6 | Welcome to the Matplotlib bakery. We will create a pie and a donut |
7 | 7 | chart through the `pie method <matplotlib.axes.Axes.pie>` and |
8 | 8 | show how to label them with a `legend <matplotlib.axes.Axes.legend>` |
9 | | -as well as with `annotations <matplotlib.axes.Axes.annotate>`. |
| 9 | +as well as with the `pie_label method <matplotlib.axes.Axes.pie>` and |
| 10 | +`annotations <matplotlib.axes.Axes.annotate>`. |
10 | 11 | """ |
11 | 12 |
|
12 | 13 | # %% |
|
15 | 16 | # Now it's time for the pie. Starting with a pie recipe, we create the data |
16 | 17 | # and a list of labels from it. |
17 | 18 | # |
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. |
| 19 | +# We then create the pie and store the returned `~matplotlib.container.PieContainer` |
| 20 | +# object for later. |
21 | 21 | # |
22 | | -# We then create the pie and store the returned objects for later. The first |
23 | | -# returned element of the returned tuple is a list of the wedges. Those are |
| 22 | +# We can provide the `~matplotlib.container.PieContainer` and a format string to |
| 23 | +# the `~matplotlib.axes.Axes.pie_label` method to automatically label each |
| 24 | +# ingredient's wedge with its weight in grams and percentages. |
| 25 | +# |
| 26 | +# The `~.PieContainer` has a list of patches as one of its attributes. Those are |
24 | 27 | # `matplotlib.patches.Wedge` patches, which can directly be used as the handles |
25 | 28 | # for a legend. We can use the legend's ``bbox_to_anchor`` argument to position |
26 | 29 | # the legend outside of the pie. Here we use the axes coordinates ``(1, 0, 0.5, |
|
31 | 34 | import matplotlib.pyplot as plt |
32 | 35 | import numpy as np |
33 | 36 |
|
34 | | -fig, ax = plt.subplots(figsize=(6, 3), subplot_kw=dict(aspect="equal")) |
| 37 | +fig, ax = plt.subplots(figsize=(6, 3)) |
35 | 38 |
|
36 | 39 | recipe = ["375 g flour", |
37 | 40 | "75 g sugar", |
38 | 41 | "250 g butter", |
39 | 42 | "300 g berries"] |
40 | 43 |
|
41 | | -data = [float(x.split()[0]) for x in recipe] |
| 44 | +data = [int(x.split()[0]) for x in recipe] |
42 | 45 | ingredients = [x.split()[-1] for x in recipe] |
43 | 46 |
|
| 47 | +pie = ax.pie(data) |
44 | 48 |
|
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 | +ax.pie_label(pie, '{frac:.1%}\n({absval:d}g)', |
| 50 | + textprops=dict(color="w", size=8, weight="bold")) |
49 | 51 |
|
50 | | -wedges, texts, autotexts = ax.pie(data, autopct=lambda pct: func(pct, data), |
51 | | - textprops=dict(color="w")) |
52 | | - |
53 | | -ax.legend(wedges, ingredients, |
| 52 | +ax.legend(pie.wedges, ingredients, |
54 | 53 | title="Ingredients", |
55 | 54 | loc="center left", |
56 | 55 | bbox_to_anchor=(1, 0, 0.5, 1)) |
57 | 56 |
|
58 | | -plt.setp(autotexts, size=8, weight="bold") |
59 | | - |
60 | 57 | ax.set_title("Matplotlib bakery: A pie") |
61 | 58 |
|
62 | 59 | plt.show() |
@@ -97,13 +94,13 @@ def func(pct, allvals): |
97 | 94 |
|
98 | 95 | data = [225, 90, 50, 60, 100, 5] |
99 | 96 |
|
100 | | -wedges, texts = ax.pie(data, wedgeprops=dict(width=0.5), startangle=-40) |
| 97 | +pie = ax.pie(data, wedgeprops=dict(width=0.5), startangle=-40) |
101 | 98 |
|
102 | 99 | bbox_props = dict(boxstyle="square,pad=0.3", fc="w", ec="k", lw=0.72) |
103 | 100 | kw = dict(arrowprops=dict(arrowstyle="-"), |
104 | 101 | bbox=bbox_props, zorder=0, va="center") |
105 | 102 |
|
106 | | -for i, p in enumerate(wedges): |
| 103 | +for i, p in enumerate(pie.wedges): |
107 | 104 | ang = (p.theta2 - p.theta1)/2. + p.theta1 |
108 | 105 | y = np.sin(np.deg2rad(ang)) |
109 | 106 | x = np.cos(np.deg2rad(ang)) |
@@ -131,6 +128,7 @@ def func(pct, allvals): |
131 | 128 | # in this example: |
132 | 129 | # |
133 | 130 | # - `matplotlib.axes.Axes.pie` / `matplotlib.pyplot.pie` |
| 131 | +# - `matplotlib.axes.Axes.pie_label` / `matplotlib.pyplot.pie_label` |
134 | 132 | # - `matplotlib.axes.Axes.legend` / `matplotlib.pyplot.legend` |
135 | 133 | # |
136 | 134 | # .. tags:: |
|
0 commit comments