Skip to content

Commit 11d528d

Browse files
story645QuLogicdstansby
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 Co-authored-by: Elliott Sales de Andrade <quantum.analyst@gmail.com> Co-authored-by: David Stansby <dstansby@gmail.com>
1 parent a483e85 commit 11d528d

File tree

1 file changed

+89
-25
lines changed

1 file changed

+89
-25
lines changed

examples/pie_and_polar_charts/pie_features.py

Lines changed: 89 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,41 +1,105 @@
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]
28-
explode = (0, 0.1, 0, 0) # only "explode" the 2nd slice (i.e. 'Hogs')
2921

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.
22+
fig, ax = plt.subplots()
23+
ax.pie(sizes, labels=labels)
3424

35-
plt.show()
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+
# %%
32+
# Auto-label slices
33+
# -----------------
34+
#
35+
# Pass a function or format string to *autopct* to label slices.
36+
37+
fig, ax = plt.subplots()
38+
ax.pie(sizes, labels=labels, autopct='%1.1f%%')
39+
40+
#################################################################
41+
# By default, the label values are obtained from the percent size of the slice.
42+
43+
# %%
44+
# Color slices
45+
# ------------
46+
#
47+
# Pass a list of colors to *colors* to set the color of each slice.
48+
49+
fig, ax = plt.subplots()
50+
ax.pie(sizes, labels=labels,
51+
colors=['olivedrab', 'rosybrown', 'gray', 'saddlebrown'])
52+
53+
# %%
54+
# Hatch slices
55+
# ------------
56+
#
57+
# Pass a list of hatch patterns to *hatch* to set the pattern of each slice.
58+
59+
fig, ax = plt.subplots()
60+
ax.pie(sizes, labels=labels, hatch=['**O', 'oO', 'O.O', '.||.'])
61+
62+
# %%
63+
# Swap label and autopct text positions
64+
# -------------------------------------
65+
# Use the *labeldistance* and *pctdistance* parameters to position the *labels*
66+
# and *autopct* text respectively.
67+
68+
fig, ax = plt.subplots()
69+
ax.pie(sizes, labels=labels, autopct='%1.1f%%',
70+
pctdistance=1.25, labeldistance=.6)
3671

3772
#############################################################################
73+
# *labeldistance* and *pctdistance* are ratios of the radius; therefore they
74+
# vary between ``0`` for the center of the pie and ``1`` for the edge of the
75+
# pie, and can be set to greater than ``1`` to place text outside the pie.
76+
77+
# %%
78+
# Explode, shade, and rotate slices
79+
# ---------------------------------
80+
#
81+
# In addition to the basic pie chart, this demo shows a few optional features:
82+
#
83+
# * offsetting a slice using *explode*
84+
# * add a drop-shadow using *shadow*
85+
# * custom start angle using *startangle*
3886
#
87+
# This example orders the slices, separates (explodes) them, and rotates them.
88+
89+
explode = (0, 0.1, 0, 0) # only "explode" the 2nd slice (i.e. 'Hogs')
90+
91+
fig, ax = plt.subplots()
92+
ax.pie(sizes, explode=explode, labels=labels, autopct='%1.1f%%',
93+
shadow=True, startangle=90)
94+
plt.show()
95+
96+
##############################################################################
97+
# The default *startangle* is 0, which would start the first slice ("Frogs") on
98+
# the positive x-axis. This example sets ``startangle = 90`` such that all the
99+
# slices are rotated counter-clockwise by 90 degrees, and the frog slice starts
100+
# on the positive y-axis.
101+
102+
##############################################################################
39103
# .. admonition:: References
40104
#
41105
# The use of the following functions, methods, classes and modules is shown

0 commit comments

Comments
 (0)