diff --git a/examples/lines_bars_and_markers/bar_label_demo.py b/examples/lines_bars_and_markers/bar_label_demo.py index 18c2b0d1bbf3..146b6934bb48 100644 --- a/examples/lines_bars_and_markers/bar_label_demo.py +++ b/examples/lines_bars_and_markers/bar_label_demo.py @@ -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) -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') -# 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() diff --git a/examples/lines_bars_and_markers/bar_stacked.py b/examples/lines_bars_and_markers/bar_stacked.py index a5042abb1a98..81ee305e7072 100644 --- a/examples/lines_bars_and_markers/bar_stacked.py +++ b/examples/lines_bars_and_markers/bar_stacked.py @@ -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) -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() diff --git a/examples/lines_bars_and_markers/barchart.py b/examples/lines_bars_and_markers/barchart.py index 1971a5013021..f5ee6833a881 100644 --- a/examples/lines_bars_and_markers/barchart.py +++ b/examples/lines_bars_and_markers/barchart.py @@ -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()