{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "\n# Stacked bar chart\n\nThis is an example of creating a stacked bar plot\nusing `~matplotlib.pyplot.bar`.\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "import matplotlib.pyplot as plt\nimport numpy as np\n\n# data from https://allisonhorst.github.io/palmerpenguins/\n\nspecies = (\n \"Adelie\\n $\\\\mu=$3700.66g\",\n \"Chinstrap\\n $\\\\mu=$3733.09g\",\n \"Gentoo\\n $\\\\mu=5076.02g$\",\n)\nweight_counts = {\n \"Below\": np.array([70, 31, 58]),\n \"Above\": np.array([82, 37, 66]),\n}\nwidth = 0.5\n\nfig, ax = plt.subplots()\nbottom = np.zeros(3)\n\nfor boolean, weight_count in weight_counts.items():\n p = ax.bar(species, weight_count, width, label=boolean, bottom=bottom)\n bottom += weight_count\n\nax.set_title(\"Number of penguins with above average body mass\")\nax.legend(loc=\"upper right\")\n\nplt.show()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ ".. tags::\n\n plot-type: bar\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 }