.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "gallery/pie_and_polar_charts/pie_features.py" .. LINE NUMBERS ARE GIVEN BELOW. .. only:: html .. meta:: :keywords: codex .. note:: :class: sphx-glr-download-link-note :ref:`Go to the end ` to download the full example code .. rst-class:: sphx-glr-example-title .. _sphx_glr_gallery_pie_and_polar_charts_pie_features.py: .. redirect-from:: gallery/pie_and_polar_charts/pie_demo2 ========== Pie charts ========== Demo of plotting a pie chart. This example illustrates various parameters of `~matplotlib.axes.Axes.pie`. .. GENERATED FROM PYTHON SOURCE LINES 14-19 Label slices ------------ Plot a pie chart of animals and label the slices. To add labels, pass a list of labels to the *labels* parameter .. GENERATED FROM PYTHON SOURCE LINES 19-28 .. code-block:: Python import matplotlib.pyplot as plt labels = 'Frogs', 'Hogs', 'Dogs', 'Logs' sizes = [15, 30, 45, 10] fig, ax = plt.subplots() ax.pie(sizes, labels=labels) .. image-sg:: /gallery/pie_and_polar_charts/images/sphx_glr_pie_features_001.png :alt: pie features :srcset: /gallery/pie_and_polar_charts/images/sphx_glr_pie_features_001.png, /gallery/pie_and_polar_charts/images/sphx_glr_pie_features_001_2_00x.png 2.00x :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 29-38 Each slice of the pie chart is a `.patches.Wedge` object; therefore in addition to the customizations shown here, each wedge can be customized using the *wedgeprops* argument, as demonstrated in :doc:`/gallery/pie_and_polar_charts/nested_pie`. Auto-label slices ----------------- Pass a function or format string to *autopct* to label slices. .. GENERATED FROM PYTHON SOURCE LINES 38-42 .. code-block:: Python fig, ax = plt.subplots() ax.pie(sizes, labels=labels, autopct='%1.1f%%') .. image-sg:: /gallery/pie_and_polar_charts/images/sphx_glr_pie_features_002.png :alt: pie features :srcset: /gallery/pie_and_polar_charts/images/sphx_glr_pie_features_002.png, /gallery/pie_and_polar_charts/images/sphx_glr_pie_features_002_2_00x.png 2.00x :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 43-49 By default, the label values are obtained from the percent size of the slice. Color slices ------------ Pass a list of colors to *colors* to set the color of each slice. .. GENERATED FROM PYTHON SOURCE LINES 49-54 .. code-block:: Python fig, ax = plt.subplots() ax.pie(sizes, labels=labels, colors=['olivedrab', 'rosybrown', 'gray', 'saddlebrown']) .. image-sg:: /gallery/pie_and_polar_charts/images/sphx_glr_pie_features_003.png :alt: pie features :srcset: /gallery/pie_and_polar_charts/images/sphx_glr_pie_features_003.png, /gallery/pie_and_polar_charts/images/sphx_glr_pie_features_003_2_00x.png 2.00x :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 55-59 Hatch slices ------------ Pass a list of hatch patterns to *hatch* to set the pattern of each slice. .. GENERATED FROM PYTHON SOURCE LINES 59-63 .. code-block:: Python fig, ax = plt.subplots() ax.pie(sizes, labels=labels, hatch=['**O', 'oO', 'O.O', '.||.']) .. image-sg:: /gallery/pie_and_polar_charts/images/sphx_glr_pie_features_004.png :alt: pie features :srcset: /gallery/pie_and_polar_charts/images/sphx_glr_pie_features_004.png, /gallery/pie_and_polar_charts/images/sphx_glr_pie_features_004_2_00x.png 2.00x :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 64-68 Swap label and autopct text positions ------------------------------------- Use the *labeldistance* and *pctdistance* parameters to position the *labels* and *autopct* text respectively. .. GENERATED FROM PYTHON SOURCE LINES 68-73 .. code-block:: Python fig, ax = plt.subplots() ax.pie(sizes, labels=labels, autopct='%1.1f%%', pctdistance=1.25, labeldistance=.6) .. image-sg:: /gallery/pie_and_polar_charts/images/sphx_glr_pie_features_005.png :alt: pie features :srcset: /gallery/pie_and_polar_charts/images/sphx_glr_pie_features_005.png, /gallery/pie_and_polar_charts/images/sphx_glr_pie_features_005_2_00x.png 2.00x :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 74-88 *labeldistance* and *pctdistance* are ratios of the radius; therefore they vary between ``0`` for the center of the pie and ``1`` for the edge of the pie, and can be set to greater than ``1`` to place text outside the pie. Explode, shade, and rotate slices --------------------------------- In addition to the basic pie chart, this demo shows a few optional features: * offsetting a slice using *explode* * add a drop-shadow using *shadow* * custom start angle using *startangle* This example orders the slices, separates (explodes) them, and rotates them. .. GENERATED FROM PYTHON SOURCE LINES 88-96 .. code-block:: Python explode = (0, 0.1, 0, 0) # only "explode" the 2nd slice (i.e. 'Hogs') fig, ax = plt.subplots() ax.pie(sizes, explode=explode, labels=labels, autopct='%1.1f%%', shadow=True, startangle=90) plt.show() .. image-sg:: /gallery/pie_and_polar_charts/images/sphx_glr_pie_features_006.png :alt: pie features :srcset: /gallery/pie_and_polar_charts/images/sphx_glr_pie_features_006.png, /gallery/pie_and_polar_charts/images/sphx_glr_pie_features_006_2_00x.png 2.00x :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 97-107 The default *startangle* is 0, which would start the first slice ("Frogs") on the positive x-axis. This example sets ``startangle = 90`` such that all the slices are rotated counter-clockwise by 90 degrees, and the frog slice starts on the positive y-axis. Controlling the size -------------------- By changing the *radius* parameter, and often the text size for better visual appearance, the pie chart can be scaled. .. GENERATED FROM PYTHON SOURCE LINES 107-114 .. code-block:: Python fig, ax = plt.subplots() ax.pie(sizes, labels=labels, autopct='%.0f%%', textprops={'size': 'smaller'}, radius=0.5) plt.show() .. image-sg:: /gallery/pie_and_polar_charts/images/sphx_glr_pie_features_007.png :alt: pie features :srcset: /gallery/pie_and_polar_charts/images/sphx_glr_pie_features_007.png, /gallery/pie_and_polar_charts/images/sphx_glr_pie_features_007_2_00x.png 2.00x :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 115-120 Modifying the shadow -------------------- The *shadow* parameter may optionally take a dictionary with arguments to the `.Shadow` patch. This can be used to modify the default shadow. .. GENERATED FROM PYTHON SOURCE LINES 120-126 .. code-block:: Python fig, ax = plt.subplots() ax.pie(sizes, explode=explode, labels=labels, autopct='%1.1f%%', shadow={'ox': -0.04, 'edgecolor': 'none', 'shade': 0.9}, startangle=90) plt.show() .. image-sg:: /gallery/pie_and_polar_charts/images/sphx_glr_pie_features_008.png :alt: pie features :srcset: /gallery/pie_and_polar_charts/images/sphx_glr_pie_features_008.png, /gallery/pie_and_polar_charts/images/sphx_glr_pie_features_008_2_00x.png 2.00x :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 127-133 .. admonition:: References The use of the following functions, methods, classes and modules is shown in this example: - `matplotlib.axes.Axes.pie` / `matplotlib.pyplot.pie` .. rst-class:: sphx-glr-timing **Total running time of the script:** (0 minutes 2.190 seconds) .. _sphx_glr_download_gallery_pie_and_polar_charts_pie_features.py: .. only:: html .. container:: sphx-glr-footer sphx-glr-footer-example .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: pie_features.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: pie_features.py ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_