8000 DOC: Update multiple category bar chart examples by kostyafarber · Pull Request #24498 · matplotlib/matplotlib · GitHub
[go: up one dir, main page]

Skip to content

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 16 commits into from
Dec 11, 2022
Merged
Show file tree
Hide file tree
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
36 changes: 14 additions & 22 deletions examples/lines_bars_and_markers/bar_label_demo.py
Original file line number Diff line number Diff line change
Expand Up @@ -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')
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)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
bottom = np.zeros(3)
bottom = np.zeros(len(species))

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 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')
Copy link
Member

Choose a reason for hiding this comment

The 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()

Expand Down
36 changes: 20 additions & 16 deletions examples/lines_bars_and_markers/bar_stacked.py
Original file line number Diff line number Diff line change
Expand Up @@ -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$",
)
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)
Copy link
Member

Choose a reason for hiding this comment

The 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()
39 changes: 22 additions & 17 deletions examples/lines_bars_and_markers/barchart.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,31 +7,36 @@
bars with labels.
"""

# data from https://allisonhorst.github.io/palmerpenguins/

import matplotlib.pyplot as plt
import numpy as np

species = ("Adelie", "Chinstrap", "Gentoo")
penguin_means = {
'Bill Depth': (18.35, 18.43, 14.98),
'Bill Length': (38.79, 48.83, 47.50),
'Flipper Length': (189.95, 195.82, 217.19),
}

labels = ['G1', 'G2', 'G3', 'G4', 'G5']
tea_means = [20, 34, 31, 35, 27]
coffee_means = [25, 32, 34, 20, 25]

x = np.arange(len(labels)) # the label locations
x = np.arange(len(species)) # the label locations
width = 0.25 # the width of the bars
multiplier = 0

fig, ax = plt.subplots()
rects1 = ax.bar(x - width/2, tea_means, width, label='Tea')
rects2 = ax.bar(x + width/2, coffee_means, width, label='Coffee')
fig, ax = plt.subplots(constrained_layout=True)

# Add some text for labels, title and custom x-axis tick labels, etc.
ax.set_ylabel('Scores')
ax.set_title('Scores by group and beverage preferences')
ax.set_xticks(x, labels)
ax.legend()
for attribute, measurement in penguin_means.items():
offset = width * multiplier
rects = ax.bar(x + offset, measurement, width, label=attribute)
ax.bar_label(rects, padding=3)
multiplier += 1

ax.bar_label(rects1, padding=3)
ax.bar_label(rects2, padding=3)

fig.tight_layout()
# Add some text for labels, title and custom x-axis tick labels, etc.
ax.set_ylabel('Length (mm)')
ax.set_title('Penguin attributes by species')
ax.set_xticks(x + width, species)
ax.legend(loc='upper left', ncols=3)
ax.set_ylim(0, 250)

plt.show()

Expand Down
0