Grouped bar chart with labels#

This example shows a how to create a grouped bar chart and how to annotate bars with labels.

import matplotlib.pyplot as plt
import numpy as np


labels = ['G1', 'G2', 'G3', 'G4', 'G5']
tea_means = [20, 34, 31, 35, 27]
coffee_means = [25, 32, 34, 20, 25]

x = np.arange(len(labels))  # the label locations
width = 0.25  # the width of the bars

fig, ax = plt.subplots()
rects1 = ax.bar(x - width/2, tea_means, width, label='Tea')
rects2 = ax.bar(x + width/2, coffee_means, width, label='Coffee')

# Add some text for labels, title and custom x-axis tick labels, etc.
ax.set_ylabel('Scores')
ax.set_title('Scores by group and beverage preferences')
ax.set_xticks(x, labels)
ax.legend()

ax.bar_label(rects1, padding=3)
ax.bar_label(rects2, padding=3)

fig.tight_layout()

plt.show()
Scores by group and beverage preferences

References

The use of the following functions, methods, classes and modules is shown in this example:

Gallery generated by Sphinx-Gallery