Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit dcd253e

Browse files
committed
Simplify "Grouped bar chart with labels" using grouped_bar()
1 parent 13105c8 commit dcd253e

File tree

1 file changed

+4
-12
lines changed

1 file changed

+4
-12
lines changed

galleries/examples/lines_bars_and_markers/barchart.py

Lines changed: 4 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@
1010
# data from https://allisonhorst.github.io/palmerpenguins/
1111

1212
import matplotlib.pyplot as plt
13-
import numpy as np
1413

1514
species = ("Adelie", "Chinstrap", "Gentoo")
1615
penguin_means = {
@@ -19,22 +18,15 @@
1918
'Flipper Length': (189.95, 195.82, 217.19),
2019
}
2120

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

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
23+
res = ax.grouped_bar(penguin_means, tick_labels=species, group_spacing=1)
24+
for container in res.bar_containers:
25+
ax.bar_label(container, padding=3)
3326

34-
# Add some text for labels, title and custom x-axis tick labels, etc.
27+
# Add some text for labels, title, etc.
3528
ax.set_ylabel('Length (mm)')
3629
ax.set_title('Penguin attributes by species')
37-
ax.set_xticks(x + width, species)
3830
ax.legend(loc='upper left', ncols=3)
3931
ax.set_ylim(0, 250)
4032

0 commit comments

Comments
 (0)