{
  "cells": [
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\n# Bar of pie\n\nMake a \"bar of pie\" chart where the first slice of the pie is\n\"exploded\" into a bar chart with a further breakdown of said slice's\ncharacteristics. The example demonstrates using a figure with multiple\nsets of Axes and using the Axes patches list to add two ConnectionPatches\nto link the subplot charts.\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "import matplotlib.pyplot as plt\nimport numpy as np\n\nfrom matplotlib.patches import ConnectionPatch\n\n# make figure and assign axis objects\nfig, (ax1, ax2) = plt.subplots(1, 2, figsize=(9, 5))\nfig.subplots_adjust(wspace=0)\n\n# pie chart parameters\noverall_ratios = [.27, .56, .17]\nlabels = ['Approve', 'Disapprove', 'Undecided']\nexplode = [0.1, 0, 0]\n# rotate so that first wedge is split by the x-axis\nangle = -180 * overall_ratios[0]\npie = ax1.pie(overall_ratios, autopct='%1.1f%%', startangle=angle,\n              labels=labels, explode=explode)\n\n# bar chart parameters\nage_ratios = [.33, .54, .07, .06]\nage_labels = ['Under 35', '35-49', '50-65', 'Over 65']\nbottom = 1\nwidth = .2\n\n# Adding from the top matches the legend.\nfor j, (height, label) in enumerate(reversed([*zip(age_ratios, age_labels)])):\n    bottom -= height\n    bc = ax2.bar(0, height, width, bottom=bottom, color='C0', label=label,\n                 alpha=0.1 + 0.25 * j)\n    ax2.bar_label(bc, labels=[f\"{height:.0%}\"], label_type='center')\n\nax2.set_title('Age of approvers')\nax2.legend()\nax2.axis('off')\nax2.set_xlim(- 2.5 * width, 2.5 * width)\n\n# use ConnectionPatch to draw lines between the two plots\ntheta1, theta2 = pie.wedges[0].theta1, pie.wedges[0].theta2\ncenter, r = pie.wedges[0].center, pie.wedges[0].r\nbar_height = sum(age_ratios)\n\n# draw top connecting line\nx = r * np.cos(np.pi / 180 * theta2) + center[0]\ny = r * np.sin(np.pi / 180 * theta2) + center[1]\ncon = ConnectionPatch(xyA=(-width / 2, bar_height), coordsA=ax2.transData,\n                      xyB=(x, y), coordsB=ax1.transData)\ncon.set_color([0, 0, 0])\ncon.set_linewidth(4)\nax2.add_artist(con)\n\n# draw bottom connecting line\nx = r * np.cos(np.pi / 180 * theta1) + center[0]\ny = r * np.sin(np.pi / 180 * theta1) + center[1]\ncon = ConnectionPatch(xyA=(-width / 2, 0), coordsA=ax2.transData,\n                      xyB=(x, y), coordsB=ax1.transData)\ncon.set_color([0, 0, 0])\nax2.add_artist(con)\ncon.set_linewidth(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.bar` / `matplotlib.pyplot.bar`\n   - `matplotlib.axes.Axes.pie` / `matplotlib.pyplot.pie`\n   - `matplotlib.patches.ConnectionPatch`\n\n.. tags::\n\n   component: subplot\n   plot-type: pie\n   plot-type: bar\n   level: intermediate\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.14.7"
    }
  },
  "nbformat": 4,
  "nbformat_minor": 0
}