Skip to content

Commit 619804a

Browse files
committed
Cleanup bar_stacked, bar_unit_demo examples.
Use label=... to define legend labels (note that in the case here, `plt.legend((p1[0], p2[0]), (..., ...))` is not even that nice as it exposes the internals of the bar object, so using `label=...` is clearly simpler). Pep8ify variables.
1 parent c1a3c03 commit 619804a

File tree

2 files changed

+16
-17
lines changed

2 files changed

+16
-17
lines changed

examples/lines_bars_and_markers/bar_stacked.py

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -7,29 +7,28 @@
77
using `~matplotlib.pyplot.bar`. Note the parameters *yerr* used for
88
error bars, and *bottom* to stack the women's bars on top of the men's
99
bars.
10-
1110
"""
1211

1312
import numpy as np
1413
import matplotlib.pyplot as plt
1514

1615

1716
N = 5
18-
menMeans = (20, 35, 30, 35, 27)
19-
womenMeans = (25, 32, 34, 20, 25)
20-
menStd = (2, 3, 4, 1, 2)
21-
womenStd = (3, 5, 2, 3, 3)
17+
men_means = [20, 35, 30, 35, 27]
18+
women_means = [25, 32, 34, 20, 25]
19+
men_std = [2, 3, 4, 1, 2]
20+
women_std = [3, 5, 2, 3, 3]
2221
ind = np.arange(N) # the x locations for the groups
2322
width = 0.35 # the width of the bars: can also be len(x) sequence
2423

25-
p1 = plt.bar(ind, menMeans, width, yerr=menStd)
26-
p2 = plt.bar(ind, womenMeans, width,
27-
bottom=menMeans, yerr=womenStd)
24+
plt.bar(ind, men_means, width, yerr=men_std, label='Men')
25+
plt.bar(ind, women_means, width, yerr=women_std, bottom=men_means,
26+
label='Women')
2827

2928
plt.ylabel('Scores')
3029
plt.title('Scores by group and gender')
3130
plt.xticks(ind, ('G1', 'G2', 'G3', 'G4', 'G5'))
3231
plt.yticks(np.arange(0, 81, 10))
33-
plt.legend((p1[0], p2[0]), ('Men', 'Women'))
32+
plt.legend()
3433

3534
plt.show()

examples/units/bar_unit_demo.py

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -18,25 +18,25 @@
1818

1919

2020
N = 5
21-
menMeans = (150*cm, 160*cm, 146*cm, 172*cm, 155*cm)
22-
menStd = (20*cm, 30*cm, 32*cm, 10*cm, 20*cm)
21+
men_means = [150*cm, 160*cm, 146*cm, 172*cm, 155*cm]
22+
men_std = [20*cm, 30*cm, 32*cm, 10*cm, 20*cm]
2323

2424
fig, ax = plt.subplots()
2525

2626
ind = np.arange(N) # the x locations for the groups
2727
width = 0.35 # the width of the bars
28-
p1 = ax.bar(ind, menMeans, width, bottom=0*cm, yerr=menStd)
28+
ax.bar(ind, men_means, width, bottom=0*cm, yerr=men_std, label='Men')
2929

30-
31-
womenMeans = (145*cm, 149*cm, 172*cm, 165*cm, 200*cm)
32-
womenStd = (30*cm, 25*cm, 20*cm, 31*cm, 22*cm)
33-
p2 = ax.bar(ind + width, womenMeans, width, bottom=0*cm, yerr=womenStd)
30+
women_means = (145*cm, 149*cm, 172*cm, 165*cm, 200*cm)
31+
women_std = (30*cm, 25*cm, 20*cm, 31*cm, 22*cm)
32+
ax.bar(ind + width, women_means, width, bottom=0*cm, yerr=women_std,
33+
label='Women')
3434

3535
ax.set_title('Scores by group and gender')
3636
ax.set_xticks(ind + width / 2)
3737
ax.set_xticklabels(('G1', 'G2', 'G3', 'G4', 'G5'))
3838

39-
ax.legend((p1[0], p2[0]), ('Men', 'Women'))
39+
ax.legend()
4040
ax.yaxis.set_units(inch)
4141
ax.autoscale_view()
4242

0 commit comments

Comments
 (0)