|
15 | 15 | # ------------
|
16 | 16 | #
|
17 | 17 | # 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. |
19 | 19 |
|
20 | 20 | import matplotlib.pyplot as plt
|
21 | 21 |
|
22 | 22 | labels = 'Frogs', 'Hogs', 'Dogs', 'Logs'
|
23 |
| -sizes = [15, 30, 45, 10] |
| 23 | +sizes = [10, 25, 40, 5] |
24 | 24 |
|
25 | 25 | fig, ax = plt.subplots()
|
26 |
| -ax.pie(sizes, labels=labels) |
| 26 | +ax.pie(sizes, wedge_labels=labels) |
27 | 27 |
|
28 | 28 | # %%
|
29 | 29 | # Each slice of the pie chart is a `.patches.Wedge` object; therefore in
|
30 | 30 | # addition to the customizations shown here, each wedge can be customized using
|
31 | 31 | # the *wedgeprops* argument, as demonstrated in
|
32 | 32 | # :doc:`/gallery/pie_and_polar_charts/nested_pie`.
|
33 | 33 | #
|
| 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 | +# |
34 | 44 | # Auto-label slices
|
35 | 45 | # -----------------
|
36 | 46 | #
|
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. |
38 | 62 |
|
39 | 63 | 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]) |
41 | 77 |
|
42 | 78 | # %%
|
43 |
| -# By default, the label values are obtained from the percent size of the slice. |
44 | 79 | #
|
45 | 80 | # Color slices
|
46 | 81 | # ------------
|
47 | 82 | #
|
48 | 83 | # Pass a list of colors to *colors* to set the color of each slice.
|
49 | 84 |
|
50 | 85 | fig, ax = plt.subplots()
|
51 |
| -ax.pie(sizes, labels=labels, |
| 86 | +ax.pie(sizes, wedge_labels=labels, wedge_label_distance=1.1, |
52 | 87 | colors=['olivedrab', 'rosybrown', 'gray', 'saddlebrown'])
|
53 | 88 |
|
54 | 89 | # %%
|
|
58 | 93 | # Pass a list of hatch patterns to *hatch* to set the pattern of each slice.
|
59 | 94 |
|
60 | 95 | 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', '.||.']) |
72 | 98 |
|
73 | 99 | # %%
|
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. |
77 | 100 | #
|
78 | 101 | # Explode, shade, and rotate slices
|
79 | 102 | # ---------------------------------
|
|
89 | 112 | explode = (0, 0.1, 0, 0) # only "explode" the 2nd slice (i.e. 'Hogs')
|
90 | 113 |
|
91 | 114 | 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) |
94 | 117 | plt.show()
|
95 | 118 |
|
96 | 119 | # %%
|
|
107 | 130 |
|
108 | 131 | fig, ax = plt.subplots()
|
109 | 132 |
|
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], |
111 | 135 | textprops={'size': 'small'}, radius=0.5)
|
112 | 136 | plt.show()
|
113 | 137 |
|
|
119 | 143 | # the `.Shadow` patch. This can be used to modify the default shadow.
|
120 | 144 |
|
121 | 145 | 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], |
123 | 148 | shadow={'ox': -0.04, 'edgecolor': 'none', 'shade': 0.9}, startangle=90)
|
124 | 149 | plt.show()
|
125 | 150 |
|
|
0 commit comments