Skip to content

Commit f2636de

Browse files
committed
expanded basic pie example
added small examples of labels, autopct, colors, hatch, and distance in an attempt to scaffold up to exploding example addresses some of #24789
1 parent a483e85 commit f2636de

File tree

1 file changed

+86
-23
lines changed

1 file changed

+86
-23
lines changed

examples/pie_and_polar_charts/pie_features.py

Lines changed: 86 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -3,39 +3,102 @@
33
Basic pie chart
44
===============
55
6-
Demo of a basic pie chart plus a few additional features.
7-
8-
In addition to the basic pie chart, this demo shows a few optional features:
9-
10-
* slice labels
11-
* auto-labeling the percentage
12-
* offsetting a slice with "explode"
13-
* drop-shadow
14-
* custom start angle
6+
Demo of plotting a `~matplotlib.axes.Axes.pie` chart
7+
"""
158

16-
Note about the custom start angle:
9+
# %%
10+
# Label slices
11+
# ----------------
12+
#
13+
# Plot a pie chart of animals and label the slices. To add
14+
# labels, pass a list of labels to the *labels* keyword argument
1715

18-
The default ``startangle`` is 0, which would start the "Frogs" slice on the
19-
positive x-axis. This example sets ``startangle = 90`` such that everything is
20-
rotated counter-clockwise by 90 degrees, and the frog slice starts on the
21-
positive y-axis.
22-
"""
2316
import matplotlib.pyplot as plt
24-
25-
# Pie chart, where the slices will be ordered and plotted counter-clockwise:
2617
labels = 'Frogs', 'Hogs', 'Dogs', 'Logs'
2718
sizes = [15, 30, 45, 10]
28-
explode = (0, 0.1, 0, 0) # only "explode" the 2nd slice (i.e. 'Hogs')
2919

30-
fig1, ax1 = plt.subplots()
31-
ax1.pie(sizes, explode=explode, labels=labels, autopct='%1.1f%%',
32-
shadow=True, startangle=90)
33-
ax1.axis('equal') # Equal aspect ratio ensures that pie is drawn as a circle.
20+
fig, ax = plt.subplots()
21+
ax.pie(sizes, labels=labels)
3422

35-
plt.show()
23+
###############################################################################
24+
# Each slice of the pie chart is a `.patches.Wedge` object; therefore in
25+
# addition to the customizations shown here, each wedge can be customized as
26+
# demoed in :doc:`/gallery/pie_and_polar_charts/nested_pie`.
27+
28+
# %%
29+
# Auto-label slices
30+
# ---------------------------
31+
#
32+
# The pie function can use a format string or function passed to *autopct* to
33+
# label slices.
34+
35+
fig, ax = plt.subplots()
36+
ax.pie(sizes, labels=labels, autopct='%1.1f%%')
37+
38+
#################################################################
39+
# By default, the label values are obtained from the percent size of the slice.
40+
41+
# %%
42+
# Color slices
43+
# ----------------
44+
#
45+
# Pass a list of colors to *colors* to set the color of each slice.
46+
47+
fig, ax = plt.subplots()
48+
ax.pie(sizes, labels=labels,
49+
colors=['olivedrab', 'rosybrown', 'gray', 'saddlebrown'])
50+
51+
# %%
52+
# Hatch slices
53+
# ----------------
54+
#
55+
# Pass a list of hatch patterns to *hatch* to set the pattern of each slice.
56+
57+
fig, ax = plt.subplots()
58+
ax.pie(sizes, labels=labels, hatch=['**O', 'oO', 'O.O', '.||.'])
59+
60+
# %%
61+
# Swap label and autopct text positions
62+
# -------------------------------------
63+
# Use the *labeldistance* and *pctdistance* keywords to position the *labels*
64+
# and *autopct* text respectively.
65+
66+
fig, ax = plt.subplots()
67+
ax.pie(sizes, labels=labels, autopct='%1.1f%%',
68+
pctdistance=1.25, labeldistance=.6)
3669

3770
#############################################################################
71+
# *labeldistance* and *pctdistance* are ratios computed relative to the
72+
# distance of the radius, meaning that a distance of ``0`` places the text
73+
# at the center of the pie while ``1`` places the text at the edge of the pie.
74+
75+
# %%
76+
# Explode, shade, and rotate slices
77+
# ---------------------------------
78+
#
79+
# In addition to the basic pie chart, this demo shows a few optional features:
80+
#
81+
# * offsetting a slice using *explode*
82+
# * add a drop-shadow using *shadow*
83+
# * custom start angle using *startangle*
3884
#
85+
# This example orders the slices, separates (explodes) them, and plots them
86+
# counter-clockwise:
87+
88+
explode = (0, 0.1, 0, 0) # only "explode" the 2nd slice (i.e. 'Hogs')
89+
90+
fig, ax = plt.subplots()
91+
ax.pie(sizes, explode=explode, labels=labels, autopct='%1.1f%%',
92+
shadow=True, startangle=90)
93+
plt.show()
94+
95+
##############################################################################
96+
# The default *startangle* is 0, which would start the "Frogs" slice on the
97+
# positive x-axis. This example sets ``startangle = 90`` such that everything
98+
# is rotated counter-clockwise by 90 degrees, and the frog slice starts on the
99+
# positive y-axis.
100+
101+
##############################################################################
39102
# .. admonition:: References
40103
#
41104
# The use of the following functions, methods, classes and modules is shown

0 commit comments

Comments
 (0)