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 924c3fe

Browse filesBrowse files
efiringMeeseeksDev[bot]
authored andcommitted
Backport PR #14126: Simplify grouped bar chart example
1 parent c32200c commit 924c3fe
Copy full SHA for 924c3fe

File tree

Expand file treeCollapse file tree

1 file changed

+17
-29
lines changed
Filter options
Expand file treeCollapse file tree

1 file changed

+17
-29
lines changed

‎examples/lines_bars_and_markers/barchart.py

Copy file name to clipboardExpand all lines: examples/lines_bars_and_markers/barchart.py
+17-29Lines changed: 17 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -3,64 +3,52 @@
33
Grouped bar chart with labels
44
=============================
55
6-
Bar charts are useful for visualizing counts, or summary statistics
7-
with error bars. This example shows a ways to create a grouped bar chart
8-
with Matplotlib and also how to annotate bars with labels.
6+
This example shows a how to create a grouped bar chart and how to annotate
7+
bars with labels.
98
"""
109

1110
import matplotlib
1211
import matplotlib.pyplot as plt
1312
import numpy as np
1413

1514

16-
men_means, men_std = (20, 35, 30, 35, 27), (2, 3, 4, 1, 2)
17-
women_means, women_std = (25, 32, 34, 20, 25), (3, 5, 2, 3, 3)
15+
labels = ['G1', 'G2', 'G3', 'G4', 'G5']
16+
men_means = [20, 34, 30, 35, 27]
17+
women_means = [25, 32, 34, 20, 25]
1818

19-
ind = np.arange(len(men_means)) # the x locations for the groups
19+
x = np.arange(len(labels)) # the label locations
2020
width = 0.35 # the width of the bars
2121

2222
fig, ax = plt.subplots()
23-
rects1 = ax.bar(ind - width/2, men_means, width, yerr=men_std,
24-
label='Men')
25-
rects2 = ax.bar(ind + width/2, women_means, width, yerr=women_std,
26-
label='Women')
23+
rects1 = ax.bar(x - width/2, men_means, width, label='Men')
24+
rects2 = ax.bar(x + width/2, women_means, width, label='Women')
2725

2826
# Add some text for labels, title and custom x-axis tick labels, etc.
2927
ax.set_ylabel('Scores')
3028
ax.set_title('Scores by group and gender')
31-
ax.set_xticks(ind)
32-
ax.set_xticklabels(('G1', 'G2', 'G3', 'G4', 'G5'))
29+
ax.set_xticks(x)
30+
ax.set_xticklabels(labels)
3331
ax.legend()
3432

3533

36-
def autolabel(rects, xpos='center'):
37-
"""
38-
Attach a text label above each bar in *rects*, displaying its height.
39-
40-
*xpos* indicates which side to place the text w.r.t. the center of
41-
the bar. It can be one of the following {'center', 'right', 'left'}.
42-
"""
43-
44-
ha = {'center': 'center', 'right': 'left', 'left': 'right'}
45-
offset = {'center': 0, 'right': 1, 'left': -1}
46-
34+
def autolabel(rects):
35+
"""Attach a text label above each bar in *rects*, displaying its height."""
4736
for rect in rects:
4837
height = rect.get_height()
4938
ax.annotate('{}'.format(height),
5039
xy=(rect.get_x() + rect.get_width() / 2, height),
51-
xytext=(offset[xpos]*3, 3), # use 3 points offset
52-
textcoords="offset points", # in both directions
53-
ha=ha[xpos], va='bottom')
40+
xytext=(0, 3), # 3 points vertical offset
41+
textcoords="offset points",
42+
ha='center', va='bottom')
5443

5544

56-
autolabel(rects1, "left")
57-
autolabel(rects2, "right")
45+
autolabel(rects1)
46+
autolabel(rects2)
5847

5948
fig.tight_layout()
6049

6150
plt.show()
6251

63-
6452
#############################################################################
6553
#
6654
# ------------

0 commit comments

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