62 lines
1.5 KiB
Python
62 lines
1.5 KiB
Python
"""
|
|
=============================
|
|
Grouped bar chart with labels
|
|
=============================
|
|
|
|
This example shows a how to create a grouped bar chart and how to annotate
|
|
bars with labels.
|
|
"""
|
|
|
|
# data from https://allisonhorst.github.io/palmerpenguins/
|
|
|
|
import matplotlib.pyplot as plt
|
|
import numpy as np
|
|
import GlobalVariable as MYSY_GV
|
|
|
|
|
|
species = ("Adelie", "Chinstrap", "Gentoo")
|
|
penguin_means = {
|
|
'Bill Depth': (18.35, 18.43, 14.98),
|
|
'Bill Length': (38.79, 48.83, 47.50),
|
|
'Flipper Length': (189.95, 195.82, 217.19),
|
|
}
|
|
|
|
x = np.arange(len(species)) # the label locations
|
|
width = 0.25 # the width of the bars
|
|
multiplier = 0
|
|
|
|
fig, ax = plt.subplots(layout='constrained')
|
|
|
|
for attribute, measurement in penguin_means.items():
|
|
offset = width * multiplier
|
|
rects = ax.bar(x + offset, measurement, width, label=attribute)
|
|
ax.bar_label(rects, padding=3)
|
|
multiplier += 1
|
|
|
|
# Add some text for labels, title and custom x-axis tick labels, etc.
|
|
ax.set_ylabel('Length (mm)')
|
|
ax.set_title('Penguin attributes by species')
|
|
ax.set_xticks(x + width, species)
|
|
ax.legend(loc='upper left', ncols=3)
|
|
ax.set_ylim(0, 250)
|
|
|
|
img_name = str(MYSY_GV.TEMPORARY_DIRECTORY_V2)+"graph.png"
|
|
plt.savefig(img_name)
|
|
plt.show()
|
|
|
|
# %%
|
|
#
|
|
# .. admonition:: References
|
|
#
|
|
# The use of the following functions, methods, classes and modules is shown
|
|
# in this example:
|
|
#
|
|
# - `matplotlib.axes.Axes.bar` / `matplotlib.pyplot.bar`
|
|
# - `matplotlib.axes.Axes.bar_label` / `matplotlib.pyplot.bar_label`
|
|
#
|
|
# .. tags::
|
|
#
|
|
# component: label
|
|
# plot-type: bar
|
|
# level: beginner
|