Skip to content

Commit

Permalink
Simplify "Grouped bar chart with labels" using grouped_bar()
Browse files Browse the repository at this point in the history
  • Loading branch information
timhoffm committed Jan 23, 2025
1 parent 8963636 commit bf2894a
Showing 1 changed file with 4 additions and 12 deletions.
16 changes: 4 additions & 12 deletions galleries/examples/lines_bars_and_markers/barchart.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
# data from https://allisonhorst.github.io/palmerpenguins/

import matplotlib.pyplot as plt
import numpy as np

species = ("Adelie", "Chinstrap", "Gentoo")
penguin_means = {
Expand All @@ -19,22 +18,15 @@
'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
res = ax.grouped_bar(penguin_means, tick_labels=species, group_spacing=1)
for container in res.bar_containers:
ax.bar_label(container, padding=3)

# Add some text for labels, title and custom x-axis tick labels, etc.
# Add some text for labels, title, 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)

Expand Down

0 comments on commit bf2894a

Please sign in to comment.