-
-
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
Merged
Merged
Changes from all commits
Commits
Show all changes
16 commits
Select commit
Hold shift + click to select a range
97a9664
change category bar chart examples to use penguin data
kostyafarber 1685000
Merge branch 'matplotlib:main' into issue-23465
kostyafarber e1ff2e6
Merge branch 'matplotlib:main' into issue-23465
kostyafarber 7d907ed
refactor bar chart examples to use loops
kostyafarber 1a8f253
Update examples/lines_bars_and_markers/bar_label_demo.py
kostyafarber df93a6f
refactor bar_stacked.py and barchart.py less incomplete data
kostyafarber e1ddc33
remove if statements from bar_label_demo and bar_stacked
kostyafarber 213b5c7
refactor barchart.py (change xticks and legend) and bar_stacked.py (c…
kostyafarber 3af7b0d
add constrained layout to figure and change ylim setting to OO style
kostyafarber d78b8a4
Update examples/lines_bars_and_markers/bar_label_demo.py
kostyafarber 2ba5c6a
Update examples/lines_bars_and_markers/bar_stacked.py
kostyafarber 0d11e6e
Update examples/lines_bars_and_markers/barchart.py
kostyafarber 27e2e48
Update examples/lines_bars_and_markers/barchart.py
kostyafarber 255fbfa
Update examples/lines_bars_and_markers/barchart.py
kostyafarber 687c6c8
refactor examples to use dictionaries and fix syntax errors
kostyafarber 1cf307e
Update examples/lines_bars_and_markers/barchart.py
kostyafarber File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) 8000 span> | ||
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') | ||
sex_counts = { | ||
'Male': np.array([73, 34, 61]), | ||
'Female': np.array([73, 34, 58]), | ||
} | ||
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) | ||
|
||
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 sex_counts.items(): | ||
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() | ||
|
||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,30 +3,34 @@ | |
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\n $\\mu=$3700.66g", | ||
"Chinstrap\n $\\mu=$3733.09g", | ||
"Gentoo\n $\\mu=5076.02g$", | ||
) | ||
kostyafarber marked this conversation as resolved.
Show resolved
Hide resolved
|
||
weight_counts = { | ||
"Below": np.array([70, 31, 58]), | ||
"Above": np.array([82, 37, 66]), | ||
} | ||
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 weight_counts.items(): | ||
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() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cann
2CEC
ot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
if you want to be even more generalt