8000 Backport PR #14126 on branch v3.1.x (Simplify grouped bar chart example) by meeseeksmachine · Pull Request #14325 · matplotlib/matplotlib · GitHub
[go: up one dir, main page]

Skip to content

Backport PR #14126 on branch v3.1.x (Simplify grouped bar chart example) #14325

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 17 additions & 29 deletions examples/lines_bars_and_markers/barchart.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,64 +3,52 @@
Grouped bar chart with labels
=============================

Bar charts are useful for visualizing counts, or summary statistics
with error bars. This example shows a ways to create a grouped bar chart
with Matplotlib and also how to annotate bars with labels.
This example shows a how to create a grouped bar chart and how to annotate
bars with labels.
"""

import matplotlib
import matplotlib.pyplot as plt
import numpy as np


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

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

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

# Add some text for labels, title and custom x-axis tick labels, etc.
ax.set_ylabel('Scores')
ax.set_title('Scores by group and gender')
ax.set_xticks(ind)
ax.set_xticklabels(('G1', 'G2', 'G3', 'G4', 'G5'))
ax.set_xticks(x)
ax.set_xticklabels(labels)
ax.legend()


def autolabel(rects, xpos='center'):
"""
Attach a text label above each bar in *rects*, displaying its height.

*xpos* indicates which side to place the text w.r.t. the center of
the bar. It can be one of the following {'center', 'right', 'left'}.
"""

ha = {'center': 'center', 'right': 'left', 'left': 'right'}
offset = {'center': 0, 'right': 1, 'left': -1}

def autolabel(rects):
"""Attach a text label above each bar in *rects*, displaying its height."""
for rect in rects:
height = rect.get_height()
ax.annotate('{}'.format(height),
xy=(rect.get_x() + rect.get_width() / 2, height),
xytext=(offset[xpos]*3, 3), # use 3 points offset
textcoords="offset points", # in both directions
ha=ha[xpos], va='bottom')
xytext=(0, 3), # 3 points vertical offset
textcoords="offset points",
ha='center', va='bottom')


autolabel(rects1, "left")
autolabel(rects2, "right")
autolabel(rects1)
autolabel(rects2)

fig.tight_layout()

plt.show()


#############################################################################
#
# ------------
Expand Down
0