|
1 | 1 | """
|
2 |
| -=============== |
3 |
| -Basic pie chart |
4 |
| -=============== |
| 2 | +========== |
| 3 | +Pie charts |
| 4 | +========== |
5 | 5 |
|
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 | +""" |
15 | 8 |
|
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 |
17 | 15 |
|
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 |
| -""" |
23 | 16 | import matplotlib.pyplot as plt
|
24 |
| - |
25 |
| -# Pie chart, where the slices will be ordered and plotted counter-clockwise: |
26 | 17 | labels = 'Frogs', 'Hogs', 'Dogs', 'Logs'
|
27 | 18 | sizes = [15, 30, 45, 10]
|
28 |
| -explode = (0, 0.1, 0, 0) # only "explode" the 2nd slice (i.e. 'Hogs') |
29 | 19 |
|
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) |
34 | 22 |
|
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) |
36 | 68 |
|
37 | 69 | #############################################################################
|
| 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* |
38 | 83 | #
|
| 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 | +############################################################################## |
39 | 101 | # .. admonition:: References
|
40 | 102 | #
|
41 | 103 | # The use of the following functions, methods, classes and modules is shown
|
|
0 commit comments