Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit dcd253e

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

File tree

1 file changed

+4
-12
lines changed
Filter options

1 file changed

+4
-12
lines changed

‎galleries/examples/lines_bars_and_markers/barchart.py

Copy file name to clipboardExpand all lines: galleries/examples/lines_bars_and_markers/barchart.py
+4-12Lines 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 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.