Skip to content

Commit 1fa25ef

Browse files
committed
Simplify "Grouped bar chart with labels" using grouped_bar()
1 parent 72d29ee commit 1fa25ef

File tree

1 file changed

+4
-11
lines changed

1 file changed

+4
-11
lines changed

galleries/examples/lines_bars_and_markers/barchart.py

Lines changed: 4 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -19,22 +19,15 @@
1919
'Flipper Length': (189.95, 195.82, 217.19),
2020
}
2121

22-
x = np.arange(len(species)) # the label locations
23-
width = 0.25 # the width of the bars
24-
multiplier = 0
25-
2622
fig, ax = plt.subplots(layout='constrained')
2723

28-
for attribute, measurement in penguin_means.items():
29-
offset = width * multiplier
30-
rects = ax.bar(x + offset, measurement, width, label=attribute)
31-
ax.bar_label(rects, padding=3)
32-
multiplier += 1
24+
res = ax.grouped_bar(penguin_means, tick_labels=species, group_spacing=1)
25+
for container in res.bar_containers:
26+
ax.bar_label(container, padding=3)
3327

34-
# Add some text for labels, title and custom x-axis tick labels, etc.
28+
# Add some text for labels, title, etc.
3529
ax.set_ylabel('Length (mm)')
3630
ax.set_title('Penguin attributes by species')
37-
ax.set_xticks(x + width, species)
3831
ax.legend(loc='upper left', ncols=3)
3932
ax.set_ylim(0, 250)
4033

0 commit comments

Comments
 (0)