|
| 1 | +""" |
| 2 | +Demo of a basic pie chart plus a few additional features. |
| 3 | +
|
| 4 | +In addition to the basic pie chart, this demo shows a few optional features: |
| 5 | +
|
| 6 | + * slice labels |
| 7 | + * auto-labeling the percentage |
| 8 | + * offsetting a slice with "explode" |
| 9 | + * drop-shadow |
| 10 | + * custom start angle |
| 11 | +
|
| 12 | +
|
| 13 | +Note about the custom start angle: |
| 14 | +
|
| 15 | +The default ``startangle`` is 0, which would start the "Frogs" slice on the |
| 16 | +x-axis. This example sets ``startangle = 90`` such that everything is rotated |
| 17 | +counter-clockwise by 90 degrees, the frog slice starts on the positive y-axis. |
| 18 | +""" |
| 19 | +import matplotlib.pyplot as plt |
| 20 | + |
| 21 | + |
| 22 | +# The slices will be ordered and plotted counter-clockwise. |
| 23 | +labels = 'Frogs', 'Hogs', 'Dogs', 'Logs' |
| 24 | +sizes = [15, 30, 45, 10] |
| 25 | +colors = ['yellowgreen', 'gold', 'lightskyblue', 'lightcoral'] |
| 26 | +explode = (0, 0.1, 0, 0) # only "explode" the 2nd slice (i.e. 'Hogs') |
| 27 | + |
| 28 | +plt.pie(sizes, explode=explode, labels=labels, colors=colors, |
| 29 | + autopct='%1.1f%%', shadow=True, startangle=90) |
| 30 | +# Set aspect ratio to be equal so that pie is drawn as a circle. |
| 31 | +plt.axis('equal') |
| 32 | +plt.show() |
0 commit comments