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 e710123

Browse filesBrowse files
authored
DOC: Update multiple category bar chart examples (#24498)
refactor category bar chart examples to use loops and penguin data
1 parent cc20d3f commit e710123
Copy full SHA for e710123

File tree

Expand file treeCollapse file tree

3 files changed

+56
-55
lines changed
Filter options
Expand file treeCollapse file tree

3 files changed

+56
-55
lines changed

‎examples/lines_bars_and_markers/bar_label_demo.py

Copy file name to clipboardExpand all lines: examples/lines_bars_and_markers/bar_label_demo.py
+14-22Lines changed: 14 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -18,35 +18,27 @@
1818
import numpy as np
1919

2020
###############################################################################
21-
# Define the data
21+
# data from https://allisonhorst.github.io/palmerpenguins/
2222

23-
N = 5
24-
coffee_means = (20, 25, -10, 32, 10)
25-
tea_means = (30, 13, -14, 21, 17)
26-
coffee_std = (3, 2, 4, 1, 2)
27-
tea_std = (4, 3, 2, 3, 5)
28-
ind = np.arange(N) # the x locations for the groups
29-
width = 0.25 # the width of the bars: can also be len(x) sequence
23+
species = ('Adelie', 'Chinstrap', 'Gentoo')
24+
sex_counts = {
25+
'Male': np.array([73, 34, 61]),
26+
'Female': np.array([73, 34, 58]),
27+
}
28+
width = 0.6 # the width of the bars: can also be len(x) sequence
3029

31-
###############################################################################
32-
# Stacked bar plot with error bars
3330

3431
fig, ax = plt.subplots()
32+
bottom = np.zeros(3)
3533

36-
p1 = ax.bar(ind, coffee_means, width, yerr=coffee_std, label='Coffee')
37-
p2 = ax.bar(ind, tea_means, width,
38-
bottom=coffee_means, yerr=tea_std, label='Tea')
34+
for sex, sex_count in sex_counts.items():
35+
p = ax.bar(species, sex_count, width, label=sex, bottom=bottom)
36+
bottom += sex_count
3937

40-
ax.axhline(0, color='grey', linewidth=0.8)
41-
ax.set_ylabel('Scores')
42-
ax.set_title('Scores by group and their beverage choices')
43-
ax.set_xticks(ind, labels=['G1', 'G2', 'G3', 'G4', 'G5'])
44-
ax.legend()
38+
ax.bar_label(p, label_type='center')
4539

46-
# Label with label_type 'center' instead of the default 'edge'
47-
ax.bar_label(p1, label_type='center')
48-
ax.bar_label(p2, label_type='center')
49-
ax.bar_label(p2)
40+
ax.set_title('Number of penguins by sex')
41+
ax.legend()
5042

5143
plt.show()
5244

‎examples/lines_bars_and_markers/bar_stacked.py

Copy file name to clipboardExpand all lines: examples/lines_bars_and_markers/bar_stacked.py
+20-16Lines changed: 20 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -3,30 +3,34 @@
33
Stacked bar chart
44
=================
55
6-
This is an example of creating a stacked bar plot with error bars
7-
using `~matplotlib.pyplot.bar`. Note the parameters *yerr* used for
8-
error bars, and *bottom* to stack the coffee's bars on top of the tea's
9-
bars.
6+
This is an example of creating a stacked bar plot
7+
using `~matplotlib.pyplot.bar`.
108
"""
119

1210
import matplotlib.pyplot as plt
11+
import numpy as np
1312

13+
# data from https://allisonhorst.github.io/palmerpenguins/
1414

15-
labels = ['G1', 'G2', 'G3', 'G4', 'G5']
16-
tea_means = [20, 35, 30, 35, 27]
17-
coffee_means = [25, 32, 34, 20, 25]
18-
tea_std = [2, 3, 4, 1, 2]
19-
coffee_std = [3, 5, 5, 3, 3]
20-
width = 0.25 # the width of the bars: can also be len(x) sequence
15+
species = (
16+
"Adelie\n $\\mu=$3700.66g",
17+
"Chinstrap\n $\\mu=$3733.09g",
18+
"Gentoo\n $\\mu=5076.02g$",
19+
)
20+
weight_counts = {
21+
"Below": np.array([70, 31, 58]),
22+
"Above": np.array([82, 37, 66]),
23+
}
24+
width = 0.5
2125

2226
fig, ax = plt.subplots()
27+
bottom = np.zeros(3)
2328

24-
ax.bar(labels, tea_means, width, yerr=tea_std, label='Tea')
25-
ax.bar(labels, coffee_means, width, yerr=coffee_std, bottom=tea_means,
26-
label='Coffee')
29+
for boolean, weight_count in weight_counts.items():
30+
p = ax.bar(species, weight_count, width, label=boolean, bottom=bottom)
31+
bottom += weight_count
2732

28-
ax.set_ylabel('Scores')
29-
ax.set_title('Scores by group and beverage preferences')
30-
ax.legend()
33+
ax.set_title("Number of penguins with above average body mass")
34+
ax.legend(loc="upper right")
3135

3236
plt.show()

‎examples/lines_bars_and_markers/barchart.py

Copy file name to clipboardExpand all lines: examples/lines_bars_and_markers/barchart.py
+22-17Lines changed: 22 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -7,31 +7,36 @@
77
bars with labels.
88
"""
99

10+
# data from https://allisonhorst.github.io/palmerpenguins/
11+
1012
import matplotlib.pyplot as plt
1113
import numpy as np
1214

15+
species = ("Adelie", "Chinstrap", "Gentoo")
16+
penguin_means = {
17+
'Bill Depth': (18.35, 18.43, 14.98),
18+
'Bill Length': (38.79, 48.83, 47.50),
19+
'Flipper Length': (189.95, 195.82, 217.19),
20+
}
1321

14-
labels = ['G1', 'G2', 'G3', 'G4', 'G5']
15-
tea_means = [20, 34, 31, 35, 27]
16-
coffee_means = [25, 32, 34, 20, 25]
17-
18-
x = np.arange(len(labels)) # the label locations
22+
x = np.arange(len(species)) # the label locations
1923
width = 0.25 # the width of the bars
24+
multiplier = 0
2025

21-
fig, ax = plt.subplots()
22-
rects1 = ax.bar(x - width/2, tea_means, width, label='Tea')
23-
rects2 = ax.bar(x + width/2, coffee_means, width, label='Coffee')
26+
fig, ax = plt.subplots(constrained_layout=True)
2427

25-
# Add some text for labels, title and custom x-axis tick labels, etc.
26-
ax.set_ylabel('Scores')
27-
ax.set_title('Scores by group and beverage preferences')
28-
ax.set_xticks(x, labels)
29-
ax.legend()
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
3033

31-
ax.bar_label(rects1, padding=3)
32-
ax.bar_label(rects2, padding=3)
33-
34-
fig.tight_layout()
34+
# Add some text for labels, title and custom x-axis tick labels, etc.
35+
ax.set_ylabel('Length (mm)')
36+
ax.set_title('Penguin attributes by species')
37+
ax.set_xticks(x + width, species)
38+
ax.legend(loc='upper left', ncols=3)
39+
ax.set_ylim(0, 250)
3540

3641
plt.show()
3742

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.