-
-
Notifications
You must be signed in to change notification settings - Fork 7.9k
DOC: Update multiple category bar chart examples #24498
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
Changes from 7 commits
97a9664
1685000
e1ff2e6
7d907ed
1a8f253
df93a6f
e1ddc33
213b5c7
3af7b0d
d78b8a4
2ba5c6a
0d11e6e
27e2e48
255fbfa
687c6c8
1cf307e
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -18,35 +18,27 @@ | |||||
import numpy as np | ||||||
|
||||||
############################################################################### | ||||||
# Define the data | ||||||
# data from https://allisonhorst.github.io/palmerpenguins/ | ||||||
|
||||||
N = 5 | ||||||
coffee_means = (20, 25, -10, 32, 10) | ||||||
tea_means = (30, 13, -14, 21, 17) | ||||||
coffee_std = (3, 2, 4, 1, 2) | ||||||
tea_std = (4, 3, 2, 3, 5) | ||||||
ind = np.arange(N) # the x locations for the groups | ||||||
width = 0.25 # the width of the bars: can also be len(x) sequence | ||||||
species = ('Adelie', 'Chinstrap', 'Gentoo') | ||||||
sexes = ["Male", "Female"] | ||||||
male = np.array([73, 34, 61]) | ||||||
female = np.array([73, 34, 58]) | ||||||
sex_counts = [male, female] | ||||||
width = 0.6 # the width of the bars: can also be len(x) sequence | ||||||
|
||||||
############################################################################### | ||||||
# Stacked bar plot with error bars | ||||||
|
||||||
fig, ax = plt.subplots() | ||||||
bottom = np.zeros(3) | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
if you want to be even more generalt |
||||||
|
||||||
p1 = ax.bar(ind, coffee_means, width, yerr=coffee_std, label='Coffee') | ||||||
p2 = ax.bar(ind, tea_means, width, | ||||||
bottom=coffee_means, yerr=tea_std, label='Tea') | ||||||
for sex, sex_count in zip(sexes, sex_counts): | ||||||
kostyafarber marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||
p = ax.bar(species, sex_count, width, label=sex, bottom=bottom) | ||||||
bottom += sex_count | ||||||
|
||||||
ax.axhline(0, color='grey', linewidth=0.8) | ||||||
ax.set_ylabel('Scores') | ||||||
ax.set_title('Scores by group and their beverage choices') | ||||||
ax.set_xticks(ind, labels=['G1', 'G2', 'G3', 'G4', 'G5']) | ||||||
ax.legend() | ||||||
ax.bar_label(p, label_type='center') | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. is white or a gray easier to read? |
||||||
|
||||||
# Label with label_type 'center' instead of the default 'edge' | ||||||
ax.bar_label(p1, label_type='center') | ||||||
ax.bar_label(p2, label_type='center') | ||||||
ax.bar_label(p2) | ||||||
ax.set_title('Number of penguins by sex') | ||||||
ax.legend() | ||||||
|
||||||
plt.show() | ||||||
|
||||||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,30 +3,30 @@ | |
Stacked bar chart | ||
================= | ||
|
||
This is an example of creating a stacked bar plot with error bars | ||
using `~matplotlib.pyplot.bar`. Note the parameters *yerr* used for | ||
error bars, and *bottom* to stack the coffee's bars on top of the tea's | ||
bars. | ||
This is an example of creating a stacked bar plot | ||
using `~matplotlib.pyplot.bar`. | ||
""" | ||
|
||
import matplotlib.pyplot as plt | ||
import numpy as np | ||
|
||
# data from https://allisonhorst.github.io/palmerpenguins/ | ||
|
||
labels = ['G1', 'G2', 'G3', 'G4', 'G5'] | ||
tea_means = [20, 35, 30, 35, 27] | ||
coffee_means = [25, 32, 34, 20, 25] | ||
tea_std = [2, 3, 4, 1, 2] | ||
coffee_std = [3, 5, 5, 3, 3] | ||
width = 0.25 # the width of the bars: can also be len(x) sequence | ||
species = ("Adelie", "Gentoo", "Chinstrap") | ||
booleans = ["True", "False"] | ||
kostyafarber marked this conversation as resolved.
Show resolved
Hide resolved
|
||
above_average_weight = np.array([70, 31, 58]) | ||
below_average_weight = np.array([82, 37, 66]) | ||
weight_counts = [above_average_weight, below_average_weight] | ||
width = 0.5 | ||
|
||
fig, ax = plt.subplots() | ||
bottom = np.zeros(3) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. same as above for generalization |
||
|
||
ax.bar(labels, tea_means, width, yerr=tea_std, label='Tea') | ||
ax.bar(labels, coffee_means, width, yerr=coffee_std, bottom=tea_means, | ||
label='Coffee') | ||
for boolean, weight_count in zip(booleans, weight_counts): | ||
kostyafarber marked this conversation as resolved.
Show resolved
Hide resolved
|
||
p = ax.bar(species, weight_count, width, label=boolean, bottom=bottom) | ||
bottom += weight_count | ||
|
||
ax.set_ylabel('Scores') | ||
ax.set_title('Scores by group and beverage preferences') | ||
ax.legend() | ||
ax.set_title("Number of penguins with above average body mass") | ||
ax.legend(loc="upper right") | ||
|
||
plt.show() |
Uh oh!
There was an error while loading. Please reload this page.