1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42
|
"""
=================
Stacked bar chart
=================
This is an example of creating a stacked bar plot
using `~matplotlib.pyplot.bar`.
"""
import matplotlib.pyplot as plt
import numpy as np
# data from https://allisonhorst.github.io/palmerpenguins/
species = (
"Adelie\n $\\mu=$3700.66g",
"Chinstrap\n $\\mu=$3733.09g",
"Gentoo\n $\\mu=5076.02g$",
)
weight_counts = {
"Below": np.array([70, 31, 58]),
"Above": np.array([82, 37, 66]),
}
width = 0.5
fig, ax = plt.subplots()
bottom = np.zeros(3)
for boolean, weight_count in weight_counts.items():
p = ax.bar(species, weight_count, width, label=boolean, bottom=bottom)
bottom += weight_count
ax.set_title("Number of penguins with above average body mass")
ax.legend(loc="upper right")
plt.show()
# %%
# .. tags::
#
# plot-type: bar
# level: beginner
|