|
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. |
| 6 | +Demo of plotting a pie chart. |
7 | 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 |
| 8 | +This example illustrates various parameters of `~matplotlib.axes.Axes.pie`. |
| 9 | +""" |
15 | 10 |
|
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 |
17 | 17 |
|
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 | 18 | import matplotlib.pyplot as plt
|
24 |
| - |
25 |
| -# Pie chart, where the slices will be ordered and plotted counter-clockwise: |
26 | 19 | labels = 'Frogs', 'Hogs', 'Dogs', 'Logs'
|
27 | 20 | sizes = [15, 30, 45, 10]
|
28 |
| -explode = (0, 0.1, 0, 0) # only "explode" the 2nd slice (i.e. 'Hogs') |
29 | 21 |
|
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) |
34 | 24 |
|
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) |
36 | 71 |
|
37 | 72 | #############################################################################
|
| 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* |
38 | 86 | #
|
| 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 | +############################################################################## |
39 | 103 | # .. admonition:: References
|
40 | 104 | #
|
41 | 105 | # The use of the following functions, methods, classes and modules is shown
|
|
0 commit comments