Skip to content

Commit 7df007e

Browse files
story645QuLogic
andcommitted
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>
1 parent a483e85 commit 7df007e

File tree

1 file changed

+88
-26
lines changed

1 file changed

+88
-26
lines changed

examples/pie_and_polar_charts/pie_features.py

+88-26
Original file line numberDiff line numberDiff line change
@@ -1,41 +1,103 @@
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.
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* parameter
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+
# demonstrated in :doc:`/gallery/pie_and_polar_charts/nested_pie`.
27+
28+
# %%
29+
# Auto-label slices
30+
# -----------------
31+
#
32+
# Pass a function or format string to *autopct* to label slices.
33+
34+
fig, ax = plt.subplots()
35+
ax.pie(sizes, labels=labels, autopct='%1.1f%%')
36+
37+
#################################################################
38+
# By default, the label values are obtained from the percent size of the slice.
39+
40+
# %%
41+
# Color slices
42+
# ------------
43+
#
44+
# Pass a list of colors to *colors* to set the color of each slice.
45+
46+
fig, ax = plt.subplots()
47+
ax.pie(sizes, labels=labels,
48+
colors=['olivedrab', 'rosybrown', 'gray', 'saddlebrown'])
49+
50+
# %%
51+
# Hatch slices
52+
# ------------
53+
#
54+
# Pass a list of hatch patterns to *hatch* to set the pattern of each slice.
55+
56+
fig, ax = plt.subplots()
57+
ax.pie(sizes, labels=labels, hatch=['**O', 'oO', 'O.O', '.||.'])
58+
59+
# %%
60+
# Swap label and autopct text positions
61+
# -------------------------------------
62+
# Use the *labeldistance* and *pctdistance* parameters to position the *labels*
63+
# and *autopct* text respectively.
64+
65+
fig, ax = plt.subplots()
66+
ax.pie(sizes, labels=labels, autopct='%1.1f%%',
67+
pctdistance=1.25, labeldistance=.6)
3668

3769
#############################################################################
70+
# *labeldistance* and *pctdistance* are ratios of the radius; therefore setting
71+
# either parameter to ``0`` places the text at the center of the pie and
72+
# setting either parameter to ``1`` places the text at the edge of the pie.
73+
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*
3883
#
84+
# This example orders the slices, separates (explodes) them, and plots them
85+
# counter-clockwise:
86+
87+
explode = (0, 0.1, 0, 0) # only "explode" the 2nd slice (i.e. 'Hogs')
88+
89+
fig, ax = plt.subplots()
90+
ax.pie(sizes, explode=explode, labels=labels, autopct='%1.1f%%',
91+
shadow=True, startangle=90)
92+
plt.show()
93+
94+
##############################################################################
95+
# The default *startangle* is 0, which would start the "Frogs" slice on the
96+
# positive x-axis. This example sets ``startangle = 90`` such that everything
97+
# is rotated counter-clockwise by 90 degrees, and the frog slice starts on the
98+
# positive y-axis.
99+
100+
##############################################################################
39101
# .. admonition:: References
40102
#
41103
# The use of the following functions, methods, classes and modules is shown

0 commit comments

Comments
 (0)