{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "\n.. redirect-from:: gallery/pie_and_polar_charts/pie_demo2\n\n# Pie charts\n\nDemo of plotting a pie chart.\n\nThis example illustrates various parameters of `~matplotlib.axes.Axes.pie`.\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Label slices\n\nPlot a pie chart of animals and label the slices. To add\nlabels, pass a list of labels to the *labels* parameter\n\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "import matplotlib.pyplot as plt\n\nlabels = 'Frogs', 'Hogs', 'Dogs', 'Logs'\nsizes = [15, 30, 45, 10]\n\nfig, ax = plt.subplots()\nax.pie(sizes, labels=labels)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Each slice of the pie chart is a `.patches.Wedge` object; therefore in\naddition to the customizations shown here, each wedge can be customized using\nthe *wedgeprops* argument, as demonstrated in\n:doc:`/gallery/pie_and_polar_charts/nested_pie`.\n\n## Auto-label slices\n\nPass a function or format string to *autopct* to label slices.\n\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "fig, ax = plt.subplots()\nax.pie(sizes, labels=labels, autopct='%1.1f%%')" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "By default, the label values are obtained from the percent size of the slice.\n\n## Color slices\n\nPass a list of colors to *colors* to set the color of each slice.\n\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "fig, ax = plt.subplots()\nax.pie(sizes, labels=labels,\n colors=['olivedrab', 'rosybrown', 'gray', 'saddlebrown'])" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Hatch slices\n\nPass a list of hatch patterns to *hatch* to set the pattern of each slice.\n\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "fig, ax = plt.subplots()\nax.pie(sizes, labels=labels, hatch=['**O', 'oO', 'O.O', '.||.'])" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Swap label and autopct text positions\nUse the *labeldistance* and *pctdistance* parameters to position the *labels*\nand *autopct* text respectively.\n\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "fig, ax = plt.subplots()\nax.pie(sizes, labels=labels, autopct='%1.1f%%',\n pctdistance=1.25, labeldistance=.6)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "*labeldistance* and *pctdistance* are ratios of the radius; therefore they\nvary between ``0`` for the center of the pie and ``1`` for the edge of the\npie, and can be set to greater than ``1`` to place text outside the pie.\n\n## Explode, shade, and rotate slices\n\nIn addition to the basic pie chart, this demo shows a few optional features:\n\n* offsetting a slice using *explode*\n* add a drop-shadow using *shadow*\n* custom start angle using *startangle*\n\nThis example orders the slices, separates (explodes) them, and rotates them.\n\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "explode = (0, 0.1, 0, 0) # only \"explode\" the 2nd slice (i.e. 'Hogs')\n\nfig, ax = plt.subplots()\nax.pie(sizes, explode=explode, labels=labels, autopct='%1.1f%%',\n shadow=True, startangle=90)\nplt.show()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "The default *startangle* is 0, which would start the first slice (\"Frogs\") on\nthe positive x-axis. This example sets ``startangle = 90`` such that all the\nslices are rotated counter-clockwise by 90 degrees, and the frog slice starts\non the positive y-axis.\n\n## Controlling the size\n\nBy changing the *radius* parameter, and often the text size for better visual\nappearance, the pie chart can be scaled.\n\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "fig, ax = plt.subplots()\n\nax.pie(sizes, labels=labels, autopct='%.0f%%',\n textprops={'size': 'smaller'}, radius=0.5)\nplt.show()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Modifying the shadow\n\nThe *shadow* parameter may optionally take a dictionary with arguments to\nthe `.Shadow` patch. This can be used to modify the default shadow.\n\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "fig, ax = plt.subplots()\nax.pie(sizes, explode=explode, labels=labels, autopct='%1.1f%%',\n shadow={'ox': -0.04, 'edgecolor': 'none', 'shade': 0.9}, startangle=90)\nplt.show()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ ".. admonition:: References\n\n The use of the following functions, methods, classes and modules is shown\n in this example:\n\n - `matplotlib.axes.Axes.pie` / `matplotlib.pyplot.pie`\n\n.. tags::\n\n plot-type: pie\n level: beginner\n\n" ] } ], "metadata": { "kernelspec": { "display_name": "Python 3", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.13.2" } }, "nbformat": 4, "nbformat_minor": 0 }