{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "\n# Labeling pie charts\n\nThis example illustrates some features of the `~matplotlib.axes.Axes.pie_label`\nmethod, which adds labels to an existing pie chart created with\n`~matplotlib.axes.Axes.pie`.\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "The simplest option is to provide a list of strings to label each slice of the pie.\n\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "import matplotlib.pyplot as plt\n\ndata = [36, 24, 8, 12]\nlabels = ['spam', 'eggs', 'bacon', 'sausage']\n\nfig, ax = plt.subplots()\npie = ax.pie(data)\nax.pie_label(pie, labels)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "If you want the labels outside the pie, set a *distance* greater than 1.\nThis is the distance from the center of the pie as a fraction of its radius.\n\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "fig, ax = plt.subplots()\npie = ax.pie(data)\nax.pie_label(pie, labels, distance=1.1)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "You can also rotate the labels so they are oriented away from the pie center.\n\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "fig, ax = plt.subplots()\npie = ax.pie(data)\nax.pie_label(pie, labels, rotate=True)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Instead of explicit labels, pass a format string to label slices with their values...\n\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "fig, ax = plt.subplots()\npie = ax.pie(data)\nax.pie_label(pie, '{absval:.1f}')" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "...or with their percentages...\n\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "fig, ax = plt.subplots()\npie = ax.pie(data)\nax.pie_label(pie, '{frac:.1%}')" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "...or both.\n\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "fig, ax = plt.subplots()\npie = ax.pie(data)\nax.pie_label(pie, '{absval:d}\\n{frac:.1%}')" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Font styling can be configured by passing a dictionary to the *textprops* parameter.\n\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "fig, ax = plt.subplots()\npie = ax.pie(data)\nax.pie_label(pie, labels, textprops={'fontsize': 'large', 'color': 'white'})" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "`~matplotlib.axes.Axes.pie_label` can be called repeatedly to add multiple sets\nof labels.\n\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "fig, ax = plt.subplots()\npie = ax.pie(data)\n\nax.pie_label(pie, labels, distance=1.1)\nax.pie_label(pie, '{frac:.1%}', distance=0.7)\nax.pie_label(pie, '{absval:d}', distance=0.4)\n\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 - `matplotlib.axes.Axes.pie_label` / `matplotlib.pyplot.pie_label`\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.12.15" } }, "nbformat": 4, "nbformat_minor": 0 }