Skip to content

Commit 3309b15

Browse files
authored
Merge pull request matplotlib#24812 from story645/pie-example
[Doc] expanded basic pie example
2 parents baf6f83 + 8609ed3 commit 3309b15

File tree

1 file changed

+83
-23
lines changed

1 file changed

+83
-23
lines changed

Diff for: examples/pie_and_polar_charts/pie_features.py

+83-23
Original file line numberDiff line numberDiff line change
@@ -1,40 +1,100 @@
11
"""
2-
===============
3-
Basic pie chart
4-
===============
2+
==========
3+
Pie charts
4+
==========
55
6-
Demo of a basic pie chart plus a few additional features.
6+
Demo of plotting a pie chart.
77
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
8+
This example illustrates various parameters of `~matplotlib.axes.Axes.pie`.
9+
"""
1510

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

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-
"""
2318
import matplotlib.pyplot as plt
24-
25-
# Pie chart, where the slices will be ordered and plotted counter-clockwise:
2619
labels = 'Frogs', 'Hogs', 'Dogs', 'Logs'
2720
sizes = [15, 30, 45, 10]
21+
22+
fig, ax = plt.subplots()
23+
ax.pie(sizes, labels=labels)
24+
25+
# %%
26+
# Each slice of the pie chart is a `.patches.Wedge` object; therefore in
27+
# addition to the customizations shown here, each wedge can be customized using
28+
# the *wedgeprops* argument, as demonstrated in
29+
# :doc:`/gallery/pie_and_polar_charts/nested_pie`.
30+
#
31+
# Auto-label slices
32+
# -----------------
33+
#
34+
# Pass a function or format string to *autopct* to label slices.
35+
36+
fig, ax = plt.subplots()
37+
ax.pie(sizes, labels=labels, autopct='%1.1f%%')
38+
39+
# %%
40+
# By default, the label values are obtained from the percent size of the slice.
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* parameters 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)
69+
70+
# %%
71+
# *labeldistance* and *pctdistance* are ratios of the radius; therefore they
72+
# vary between ``0`` for the center of the pie and ``1`` for the edge of the
73+
# pie, and can be set to greater than ``1`` to place text outside the pie.
74+
#
75+
# Explode, shade, and rotate slices
76+
# ---------------------------------
77+
#
78+
# In addition to the basic pie chart, this demo shows a few optional features:
79+
#
80+
# * offsetting a slice using *explode*
81+
# * add a drop-shadow using *shadow*
82+
# * custom start angle using *startangle*
83+
#
84+
# This example orders the slices, separates (explodes) them, and rotates them.
85+
2886
explode = (0, 0.1, 0, 0) # only "explode" the 2nd slice (i.e. 'Hogs')
2987

30-
fig1, ax1 = plt.subplots()
31-
ax1.pie(sizes, explode=explode, labels=labels, autopct='%1.1f%%',
88+
fig, ax = plt.subplots()
89+
ax.pie(sizes, explode=explode, labels=labels, autopct='%1.1f%%',
3290
shadow=True, startangle=90)
33-
ax1.axis('equal') # Equal aspect ratio ensures that pie is drawn as a circle.
34-
3591
plt.show()
3692

3793
# %%
94+
# The default *startangle* is 0, which would start the first slice ("Frogs") on
95+
# the positive x-axis. This example sets ``startangle = 90`` such that all the
96+
# slices are rotated counter-clockwise by 90 degrees, and the frog slice starts
97+
# on the positive y-axis.
3898
#
3999
# .. admonition:: References
40100
#

0 commit comments

Comments
 (0)