{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "\n# Nested pie charts\n\nThe following examples show two ways to build a nested pie chart\nin Matplotlib. Such charts are often referred to as donut charts.\n\nSee also the :doc:`/gallery/specialty_plots/leftventricle_bullseye` example.\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "import matplotlib.pyplot as plt\nimport numpy as np" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "The most straightforward way to build a pie chart is to use the\n`~matplotlib.axes.Axes.pie` method.\n\nIn this case, pie takes values corresponding to counts in a group.\nWe'll first generate some fake data, corresponding to three groups.\nIn the inner circle, we'll treat each number as belonging to its\nown group. In the outer circle, we'll plot them as members of their\noriginal 3 groups.\n\nThe effect of the donut shape is achieved by setting a ``width`` to\nthe pie's wedges through the *wedgeprops* argument.\n\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "fig, ax = plt.subplots()\n\nsize = 0.3\nvals = np.array([[60., 32.], [37., 40.], [29., 10.]])\n\ntab20c = plt.color_sequences[\"tab20c\"]\nouter_colors = [tab20c[i] for i in [0, 4, 8]]\ninner_colors = [tab20c[i] for i in [1, 2, 5, 6, 9, 10]]\n\nax.pie(vals.sum(axis=1), radius=1, colors=outer_colors,\n wedgeprops=dict(width=size, edgecolor='w'))\n\nax.pie(vals.flatten(), radius=1-size, colors=inner_colors,\n wedgeprops=dict(width=size, edgecolor='w'))\n\nax.set(aspect=\"equal\", title='Pie plot with `ax.pie`')\nplt.show()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "However, you can accomplish the same output by using a bar plot on\nAxes with a polar coordinate system. This may give more flexibility on\nthe exact design of the plot.\n\nIn this case, we need to map x-values of the bar chart onto radians of\na circle. The cumulative sum of the values are used as the edges\nof the bars.\n\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "fig, ax = plt.subplots(subplot_kw=dict(projection=\"polar\"))\n\nsize = 0.3\nvals = np.array([[60., 32.], [37., 40.], [29., 10.]])\n# Normalize vals to 2 pi\nvalsnorm = vals/np.sum(vals)*2*np.pi\n# Obtain the ordinates of the bar edges\nvalsleft = np.cumsum(np.append(0, valsnorm.flatten()[:-1])).reshape(vals.shape)\n\ncmap = plt.colormaps[\"tab20c\"]\nouter_colors = cmap(np.arange(3)*4)\ninner_colors = cmap([1, 2, 5, 6, 9, 10])\n\nax.bar(x=valsleft[:, 0],\n width=valsnorm.sum(axis=1), bottom=1-size, height=size,\n color=outer_colors, edgecolor='w', linewidth=1, align=\"edge\")\n\nax.bar(x=valsleft.flatten(),\n width=valsnorm.flatten(), bottom=1-2*size, height=size,\n color=inner_colors, edgecolor='w', linewidth=1, align=\"edge\")\n\nax.set(title=\"Pie plot with `ax.bar` and polar coordinates\")\nax.set_axis_off()\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.bar` / `matplotlib.pyplot.bar`\n - `matplotlib.projections.polar`\n - ``Axes.set`` (`matplotlib.artist.Artist.set`)\n - `matplotlib.axes.Axes.set_axis_off`\n\n.. tags::\n\n plot-type: pie\n level: beginner\n purpose: showcase\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 }