From 97a9664660a76dd19f1fc327b02ecfeaf03ad03d Mon Sep 17 00:00:00 2001 From: Kostya Farber Date: Fri, 18 Nov 2022 15:27:05 +0000 Subject: [PATCH 01/14] change category bar chart examples to use penguin data --- .../lines_bars_and_markers/bar_label_demo.py | 30 +++++++------------ .../lines_bars_and_markers/bar_stacked.py | 30 +++++++++---------- examples/lines_bars_and_markers/barchart.py | 25 +++++++++------- 3 files changed, 40 insertions(+), 45 deletions(-) diff --git a/examples/lines_bars_and_markers/bar_label_demo.py b/examples/lines_bars_and_markers/bar_label_demo.py index 18c2b0d1bbf3..158a5f42df9d 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 +N = 3 +ind = np.arange(N) +species = ('Adelie', 'Chinstrap', 'Gentoo') +male = (73, 34, 61) +female = (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() -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') +p1 = ax.bar(species, male, width, label="Male") +p2 = ax.bar(species, female, width, bottom=male, label="Female") -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() - -# Label with label_type 'center' instead of the default 'edge' +ax.set_title('Number of penguins by sex') ax.bar_label(p1, label_type='center') ax.bar_label(p2, label_type='center') -ax.bar_label(p2) +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..bfc02c4d0779 100644 --- a/examples/lines_bars_and_markers/bar_stacked.py +++ b/examples/lines_bars_and_markers/bar_stacked.py @@ -3,30 +3,28 @@ 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 +# 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 +islands = ("Biscoe", "Dream", "Torgersen") +adelie_means = (44, 56, 52) +gentoo_means = (124, 0, 0) +chinstrap_means = (0, 68, 0) +width = 0.5 fig, ax = plt.subplots() -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') +p1 = ax.bar(islands, adelie_means, width, label="Adelie") +p2 = ax.bar(islands, gentoo_means, width, bottom=adelie_means, label="Gentoo") +p3 = ax.bar(islands, chinstrap_means, width, + bottom=adelie_means, label="Chinstrap") -ax.set_ylabel('Scores') -ax.set_title('Scores by group and beverage preferences') -ax.legend() +ax.set_title("Number of penguins by island") +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..83b0aac4c3dd 100644 --- a/examples/lines_bars_and_markers/barchart.py +++ b/examples/lines_bars_and_markers/barchart.py @@ -7,32 +7,37 @@ bars with labels. """ +# data from https://allisonhorst.github.io/palmerpenguins/ + import matplotlib.pyplot as plt import numpy as np +islands = ('Biscoe', 'Dream', 'Torgersen') +adelie_means = (188.80, 189.73, 191.20) +gentoo_means = (217.19, 0, 0) +chinstrap_means = (0, 48.83, 0) -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(islands)) # the label locations width = 0.25 # the width of the bars 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') +rects1 = ax.bar(x - width, adelie_means, width, label='Adelie') +rects2 = ax.bar(x, gentoo_means, width, label='Chinstrap') +rects3 = ax.bar(x + width, chinstrap_means, width, label='Gentoo') # 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.set_ylabel('Flipper length (mm)') +ax.set_title('Average flipper length of penguin species by island') +ax.set_xticks(x, islands) ax.legend() ax.bar_label(rects1, padding=3) ax.bar_label(rects2, padding=3) +ax.bar_label(rects3, padding=3) fig.tight_layout() +plt.ylim(0, 250) plt.show() ############################################################################# From 7d907edb8c3a101edb4a0006e9d250511f7a8a4a Mon Sep 17 00:00:00 2001 From: Kostya Farber Date: Sun, 20 Nov 2022 11:22:50 +0000 Subject: [PATCH 02/14] refactor bar chart examples to use loops --- .../lines_bars_and_markers/bar_label_demo.py | 18 +++++++++++------- examples/lines_bars_and_markers/bar_stacked.py | 13 +++++++++---- examples/lines_bars_and_markers/barchart.py | 16 ++++++++-------- 3 files changed, 28 insertions(+), 19 deletions(-) diff --git a/examples/lines_bars_and_markers/bar_label_demo.py b/examples/lines_bars_and_markers/bar_label_demo.py index 158a5f42df9d..046538be7ae5 100644 --- a/examples/lines_bars_and_markers/bar_label_demo.py +++ b/examples/lines_bars_and_markers/bar_label_demo.py @@ -20,24 +20,28 @@ ############################################################################### # data from https://allisonhorst.github.io/palmerpenguins/ -N = 3 -ind = np.arange(N) species = ('Adelie', 'Chinstrap', 'Gentoo') +sexes = ["Male", "Female"] male = (73, 34, 61) female = (73, 34, 58) -width = 0.6 # the width of the bars: can also be len(x) sequence +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() -p1 = ax.bar(species, male, width, label="Male") -p2 = ax.bar(species, female, width, bottom=male, label="Female") +for sex, sex_count in zip(sexes, sex_counts): + if sex == "Male": + p = ax.bar(species, sex_count, width, label=sex) + + else: + p = ax.bar(species, sex_count, width, label=sex, bottom=sex_counts[0]) + + ax.bar_label(p, label_type='center') ax.set_title('Number of penguins by sex') -ax.bar_label(p1, label_type='center') -ax.bar_label(p2, label_type='center') 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 bfc02c4d0779..bf76648685d9 100644 --- a/examples/lines_bars_and_markers/bar_stacked.py +++ b/examples/lines_bars_and_markers/bar_stacked.py @@ -12,17 +12,22 @@ # data from https://allisonhorst.github.io/palmerpenguins/ islands = ("Biscoe", "Dream", "Torgersen") +species = ["Adelie", "Gentoo", "Chinstrap"] adelie_means = (44, 56, 52) gentoo_means = (124, 0, 0) chinstrap_means = (0, 68, 0) +penguin_means = [adelie_means, gentoo_means, chinstrap_means] width = 0.5 fig, ax = plt.subplots() -p1 = ax.bar(islands, adelie_means, width, label="Adelie") -p2 = ax.bar(islands, gentoo_means, width, bottom=adelie_means, label="Gentoo") -p3 = ax.bar(islands, chinstrap_means, width, - bottom=adelie_means, label="Chinstrap") +for species, penguin_mean in zip(species, penguin_means): + if species == "Adelie": + p = ax.bar(islands, penguin_mean, width, label=species) + + else: + p = ax.bar(islands, penguin_mean, width, label=species, + bottom=penguin_means[0]) ax.set_title("Number of penguins by island") ax.legend(loc="upper right") diff --git a/examples/lines_bars_and_markers/barchart.py b/examples/lines_bars_and_markers/barchart.py index 83b0aac4c3dd..2cec90611522 100644 --- a/examples/lines_bars_and_markers/barchart.py +++ b/examples/lines_bars_and_markers/barchart.py @@ -13,17 +13,21 @@ import numpy as np islands = ('Biscoe', 'Dream', 'Torgersen') +species = ["Adelie", "Gentoo", "Chinstrap"] adelie_means = (188.80, 189.73, 191.20) gentoo_means = (217.19, 0, 0) chinstrap_means = (0, 48.83, 0) +penguin_means = [adelie_means, gentoo_means, chinstrap_means] x = np.arange(len(islands)) # the label locations width = 0.25 # the width of the bars - +multiplier = 0 fig, ax = plt.subplots() -rects1 = ax.bar(x - width, adelie_means, width, label='Adelie') -rects2 = ax.bar(x, gentoo_means, width, label='Chinstrap') -rects3 = ax.bar(x + width, chinstrap_means, width, label='Gentoo') + +for specie, penguin_mean in zip(species, penguin_means): + rects = ax.bar(x + (width * multiplier), penguin_mean, width, label=specie) + ax.bar_label(rects, padding=3) + multiplier += 1 # Add some text for labels, title and custom x-axis tick labels, etc. ax.set_ylabel('Flipper length (mm)') @@ -31,10 +35,6 @@ ax.set_xticks(x, islands) ax.legend() -ax.bar_label(rects1, padding=3) -ax.bar_label(rects2, padding=3) -ax.bar_label(rects3, padding=3) - fig.tight_layout() plt.ylim(0, 250) From 1a8f25351f1195290f60e8ed35ad3cfeeb643c60 Mon Sep 17 00:00:00 2001 From: Kostya Farber <73378227+kostyafarber@users.noreply.github.com> Date: Tue, 22 Nov 2022 10:40:39 +0000 Subject: [PATCH 03/14] Update examples/lines_bars_and_markers/bar_label_demo.py Co-authored-by: Tim Hoffmann <2836374+timhoffm@users.noreply.github.com> --- examples/lines_bars_and_markers/bar_label_demo.py | 2 -- 1 file changed, 2 deletions(-) diff --git a/examples/lines_bars_and_markers/bar_label_demo.py b/examples/lines_bars_and_markers/bar_label_demo.py index 046538be7ae5..0becf6308b6a 100644 --- a/examples/lines_bars_and_markers/bar_label_demo.py +++ b/examples/lines_bars_and_markers/bar_label_demo.py @@ -27,8 +27,6 @@ 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() From df93a6f6b334a5ac9b81bf2aee0bc91ce033b92e Mon Sep 17 00:00:00 2001 From: Kostya Farber Date: Thu, 24 Nov 2022 15:45:50 +0000 Subject: [PATCH 04/14] refactor bar_stacked.py and barchart.py less incomplete data --- .../lines_bars_and_markers/bar_stacked.py | 23 ++++++++------- examples/lines_bars_and_markers/barchart.py | 28 +++++++++---------- 2 files changed, 25 insertions(+), 26 deletions(-) diff --git a/examples/lines_bars_and_markers/bar_stacked.py b/examples/lines_bars_and_markers/bar_stacked.py index bf76648685d9..cad0ebe1e987 100644 --- a/examples/lines_bars_and_markers/bar_stacked.py +++ b/examples/lines_bars_and_markers/bar_stacked.py @@ -11,25 +11,24 @@ # data from https://allisonhorst.github.io/palmerpenguins/ -islands = ("Biscoe", "Dream", "Torgersen") -species = ["Adelie", "Gentoo", "Chinstrap"] -adelie_means = (44, 56, 52) -gentoo_means = (124, 0, 0) -chinstrap_means = (0, 68, 0) -penguin_means = [adelie_means, gentoo_means, chinstrap_means] +species = ("Adelie", "Gentoo", "Chinstrap") +booleans = ["True", "False"] +above_average_weight = (70, 31, 58) +below_average_weight = (82, 37, 66) +weight_counts = [above_average_weight, below_average_weight] width = 0.5 fig, ax = plt.subplots() -for species, penguin_mean in zip(species, penguin_means): - if species == "Adelie": - p = ax.bar(islands, penguin_mean, width, label=species) +for boolean, weight_count in zip(booleans, weight_counts): + if boolean == "True": + p = ax.bar(species, weight_count, width, label=boolean) else: - p = ax.bar(islands, penguin_mean, width, label=species, - bottom=penguin_means[0]) + p = ax.bar(species, weight_count, width, label=boolean, + bottom=weight_counts[0]) -ax.set_title("Number of penguins by island") +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 2cec90611522..9ad689a7995a 100644 --- a/examples/lines_bars_and_markers/barchart.py +++ b/examples/lines_bars_and_markers/barchart.py @@ -12,32 +12,32 @@ import matplotlib.pyplot as plt import numpy as np -islands = ('Biscoe', 'Dream', 'Torgersen') -species = ["Adelie", "Gentoo", "Chinstrap"] -adelie_means = (188.80, 189.73, 191.20) -gentoo_means = (217.19, 0, 0) -chinstrap_means = (0, 48.83, 0) -penguin_means = [adelie_means, gentoo_means, chinstrap_means] - -x = np.arange(len(islands)) # the label locations +species = ("Adelie", "Chinstrap", "Gentoo") +measurements = ['Bill Length', 'Bill Depth'] +bill_length = (38.80, 48.83, 47.50) +bill_depth = (18.34, 18.43, 14.98) +penguin_means = [bill_length, bill_depth] + +x = np.arange(len(species)) # the label locations width = 0.25 # the width of the bars multiplier = 0 fig, ax = plt.subplots() -for specie, penguin_mean in zip(species, penguin_means): - rects = ax.bar(x + (width * multiplier), penguin_mean, width, label=specie) +for measurement, penguin_mean in zip(measurements, penguin_means): + offset = (width * multiplier) + rects = ax.bar(x + offset, penguin_mean, width, label=measurement) ax.bar_label(rects, padding=3) multiplier += 1 # Add some text for labels, title and custom x-axis tick labels, etc. -ax.set_ylabel('Flipper length (mm)') -ax.set_title('Average flipper length of penguin species by island') -ax.set_xticks(x, islands) +ax.set_ylabel('Length (mm)') +ax.set_title('Penguin attributes by species') +ax.set_xticks(x + (width / 2), species) ax.legend() fig.tight_layout() -plt.ylim(0, 250) +plt.ylim(0, 60) plt.show() ############################################################################# From e1ddc33a00b9b8bbf923608f5f1ecb4750571db4 Mon Sep 17 00:00:00 2001 From: Kostya Farber Date: Thu, 24 Nov 2022 17:42:44 +0000 Subject: [PATCH 05/14] remove if statements from bar_label_demo and bar_stacked --- examples/lines_bars_and_markers/bar_label_demo.py | 12 +++++------- examples/lines_bars_and_markers/bar_stacked.py | 14 ++++++-------- 2 files changed, 11 insertions(+), 15 deletions(-) diff --git a/examples/lines_bars_and_markers/bar_label_demo.py b/examples/lines_bars_and_markers/bar_label_demo.py index 0becf6308b6a..f22d97d1d1c6 100644 --- a/examples/lines_bars_and_markers/bar_label_demo.py +++ b/examples/lines_bars_and_markers/bar_label_demo.py @@ -22,20 +22,18 @@ species = ('Adelie', 'Chinstrap', 'Gentoo') sexes = ["Male", "Female"] -male = (73, 34, 61) -female = (73, 34, 58) +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 fig, ax = plt.subplots() +bottom = np.zeros(3) for sex, sex_count in zip(sexes, sex_counts): - if sex == "Male": - p = ax.bar(species, sex_count, width, label=sex) - - else: - p = ax.bar(species, sex_count, width, label=sex, bottom=sex_counts[0]) + p = ax.bar(species, sex_count, width, label=sex, bottom=bottom) + bottom += sex_count ax.bar_label(p, label_type='center') diff --git a/examples/lines_bars_and_markers/bar_stacked.py b/examples/lines_bars_and_markers/bar_stacked.py index cad0ebe1e987..dd9c4361a0b3 100644 --- a/examples/lines_bars_and_markers/bar_stacked.py +++ b/examples/lines_bars_and_markers/bar_stacked.py @@ -8,25 +8,23 @@ """ import matplotlib.pyplot as plt +import numpy as np # data from https://allisonhorst.github.io/palmerpenguins/ species = ("Adelie", "Gentoo", "Chinstrap") booleans = ["True", "False"] -above_average_weight = (70, 31, 58) -below_average_weight = (82, 37, 66) +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) for boolean, weight_count in zip(booleans, weight_counts): - if boolean == "True": - p = ax.bar(species, weight_count, width, label=boolean) - - else: - p = ax.bar(species, weight_count, width, label=boolean, - bottom=weight_counts[0]) + p = ax.bar(species, weight_count, width, label=boolean, bottom=bottom) + bottom += weight_count ax.set_title("Number of penguins with above average body mass") ax.legend(loc="upper right") From 213b5c7160cff051bdf848e457896e8475318e03 Mon Sep 17 00:00:00 2001 From: Kostya Farber Date: Tue, 29 Nov 2022 14:58:47 +0000 Subject: [PATCH 06/14] refactor barchart.py (change xticks and legend) and bar_stacked.py (change booleans and labels) --- examples/lines_bars_and_markers/bar_stacked.py | 10 +++++++--- examples/lines_bars_and_markers/barchart.py | 15 ++++++++------- 2 files changed, 15 insertions(+), 10 deletions(-) diff --git a/examples/lines_bars_and_markers/bar_stacked.py b/examples/lines_bars_and_markers/bar_stacked.py index dd9c4361a0b3..c4765e3192db 100644 --- a/examples/lines_bars_and_markers/bar_stacked.py +++ b/examples/lines_bars_and_markers/bar_stacked.py @@ -12,11 +12,15 @@ # data from https://allisonhorst.github.io/palmerpenguins/ -species = ("Adelie", "Gentoo", "Chinstrap") -booleans = ["True", "False"] +species = ( + "Adelie\n $\\mu=$3700.66g", + "Chinstrap\n $\\mu=$3733.09g", + "Gentoo\n $\\mu=5076.02g$", +) +booleans = ["Below", "Above"] above_average_weight = np.array([70, 31, 58]) below_average_weight = np.array([82, 37, 66]) -weight_counts = [above_average_weight, below_average_weight] +weight_counts = [below_average_weight, above_average_weight] width = 0.5 fig, ax = plt.subplots() diff --git a/examples/lines_bars_and_markers/barchart.py b/examples/lines_bars_and_markers/barchart.py index 9ad689a7995a..08f6d98be10a 100644 --- a/examples/lines_bars_and_markers/barchart.py +++ b/examples/lines_bars_and_markers/barchart.py @@ -13,10 +13,11 @@ import numpy as np species = ("Adelie", "Chinstrap", "Gentoo") -measurements = ['Bill Length', 'Bill Depth'] -bill_length = (38.80, 48.83, 47.50) -bill_depth = (18.34, 18.43, 14.98) -penguin_means = [bill_length, bill_depth] +measurements = ['Bill Depth', 'Bill Length', "Flipper Length"] +bill_depth = (18.35, 18.43, 14.98) +bill_length = (38.79, 48.83, 47.50) +flipper_length = (189.95, 195.82, 217.19) +penguin_means = [bill_depth, bill_length, flipper_length] x = np.arange(len(species)) # the label locations width = 0.25 # the width of the bars @@ -32,12 +33,12 @@ # 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 / 2), species) -ax.legend() +ax.set_xticks(x + (width), species) +ax.legend(loc='upper left', ncols=3) fig.tight_layout() -plt.ylim(0, 60) +plt.ylim(0, 250) plt.show() ############################################################################# From 3af7b0d7be0f49de5719af8a570a4419f7978744 Mon Sep 17 00:00:00 2001 From: Kostya Farber Date: Fri, 2 Dec 2022 09:28:34 +0000 Subject: [PATCH 07/14] add constrained layout to figure and change ylim setting to OO style --- examples/lines_bars_and_markers/barchart.py | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/examples/lines_bars_and_markers/barchart.py b/examples/lines_bars_and_markers/barchart.py index 08f6d98be10a..15510abd93f6 100644 --- a/examples/lines_bars_and_markers/barchart.py +++ b/examples/lines_bars_and_markers/barchart.py @@ -22,7 +22,7 @@ x = np.arange(len(species)) # the label locations width = 0.25 # the width of the bars multiplier = 0 -fig, ax = plt.subplots() +fig, ax = plt.subplots(constrained_layout=True) for measurement, penguin_mean in zip(measurements, penguin_means): offset = (width * multiplier) @@ -35,10 +35,8 @@ 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) -fig.tight_layout() - -plt.ylim(0, 250) plt.show() ############################################################################# From d78b8a489b60ff2ab62995f15176acd4fcc9211a Mon Sep 17 00:00:00 2001 From: Kostya Farber <73378227+kostyafarber@users.noreply.github.com> Date: Tue, 6 Dec 2022 08:51:09 +0000 Subject: [PATCH 08/14] Update examples/lines_bars_and_markers/bar_label_demo.py Co-authored-by: Elliott Sales de Andrade --- examples/lines_bars_and_markers/bar_label_demo.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/examples/lines_bars_and_markers/bar_label_demo.py b/examples/lines_bars_and_markers/bar_label_demo.py index f22d97d1d1c6..5eefe3300634 100644 --- a/examples/lines_bars_and_markers/bar_label_demo.py +++ b/examples/lines_bars_and_markers/bar_label_demo.py @@ -21,10 +21,10 @@ # data from https://allisonhorst.github.io/palmerpenguins/ species = ('Adelie', 'Chinstrap', 'Gentoo') -sexes = ["Male", "Female"] -male = np.array([73, 34, 61]) -female = np.array([73, 34, 58]) -sex_counts = [male, female] +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 From 2ba5c6a3bbf6c09300d8c88ee788a15195742ce0 Mon Sep 17 00:00:00 2001 From: Kostya Farber <73378227+kostyafarber@users.noreply.github.com> Date: Tue, 6 Dec 2022 08:51:14 +0000 Subject: [PATCH 09/14] Update examples/lines_bars_and_markers/bar_stacked.py Co-authored-by: Elliott Sales de Andrade --- examples/lines_bars_and_markers/bar_stacked.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/examples/lines_bars_and_markers/bar_stacked.py b/examples/lines_bars_and_markers/bar_stacked.py index c4765e3192db..82cc5b2decd4 100644 --- a/examples/lines_bars_and_markers/bar_stacked.py +++ b/examples/lines_bars_and_markers/bar_stacked.py @@ -17,10 +17,10 @@ "Chinstrap\n $\\mu=$3733.09g", "Gentoo\n $\\mu=5076.02g$", ) -booleans = ["Below", "Above"] -above_average_weight = np.array([70, 31, 58]) -below_average_weight = np.array([82, 37, 66]) -weight_counts = [below_average_weight, above_average_weight] +weight_counts = { + "Below": np.array([70, 31, 58]), + "Above": np.array([82, 37, 66]), +] width = 0.5 fig, ax = plt.subplots() From 0d11e6ee3bf3f20a75ad3c8a8e8ceae2ea48ce3a Mon Sep 17 00:00:00 2001 From: Kostya Farber <73378227+kostyafarber@users.noreply.github.com> Date: Tue, 6 Dec 2022 08:51:21 +0000 Subject: [PATCH 10/14] Update examples/lines_bars_and_markers/barchart.py Co-authored-by: Elliott Sales de Andrade --- examples/lines_bars_and_markers/barchart.py | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/examples/lines_bars_and_markers/barchart.py b/examples/lines_bars_and_markers/barchart.py index 15510abd93f6..941719f2f33c 100644 --- a/examples/lines_bars_and_markers/barchart.py +++ b/examples/lines_bars_and_markers/barchart.py @@ -13,11 +13,11 @@ import numpy as np species = ("Adelie", "Chinstrap", "Gentoo") -measurements = ['Bill Depth', 'Bill Length', "Flipper Length"] -bill_depth = (18.35, 18.43, 14.98) -bill_length = (38.79, 48.83, 47.50) -flipper_length = (189.95, 195.82, 217.19) -penguin_means = [bill_depth, bill_length, flipper_length] +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), +} x = np.arange(len(species)) # the label locations width = 0.25 # the width of the bars From 27e2e4883cb947717cade887f9fcdd784e191258 Mon Sep 17 00:00:00 2001 From: Kostya Farber <73378227+kostyafarber@users.noreply.github.com> Date: Tue, 6 Dec 2022 08:51:28 +0000 Subject: [PATCH 11/14] Update examples/lines_bars_and_markers/barchart.py Co-authored-by: Elliott Sales de Andrade --- examples/lines_bars_and_markers/barchart.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/examples/lines_bars_and_markers/barchart.py b/examples/lines_bars_and_markers/barchart.py index 941719f2f33c..cc5d1f261f51 100644 --- a/examples/lines_bars_and_markers/barchart.py +++ b/examples/lines_bars_and_markers/barchart.py @@ -25,7 +25,7 @@ fig, ax = plt.subplots(constrained_layout=True) for measurement, penguin_mean in zip(measurements, penguin_means): - offset = (width * multiplier) + offset = width * multiplier rects = ax.bar(x + offset, penguin_mean, width, label=measurement) ax.bar_label(rects, padding=3) multiplier += 1 From 255fbfa2a101b6eff93dc65921f502cdd60606a3 Mon Sep 17 00:00:00 2001 From: Kostya Farber <73378227+kostyafarber@users.noreply.github.com> Date: Tue, 6 Dec 2022 08:51:34 +0000 Subject: [PATCH 12/14] Update examples/lines_bars_and_markers/barchart.py Co-authored-by: Elliott Sales de Andrade --- examples/lines_bars_and_markers/barchart.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/examples/lines_bars_and_markers/barchart.py b/examples/lines_bars_and_markers/barchart.py index cc5d1f261f51..327958a47821 100644 --- a/examples/lines_bars_and_markers/barchart.py +++ b/examples/lines_bars_and_markers/barchart.py @@ -33,7 +33,7 @@ # 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.set_xticks(x + width, species) ax.legend(loc='upper left', ncols=3) ax.set_ylim(0, 250) From 687c6c89d04e59be373ed2bbbb2121ffd839c356 Mon Sep 17 00:00:00 2001 From: Kostya Farber Date: Tue, 6 Dec 2022 19:47:00 +0000 Subject: [PATCH 13/14] refactor examples to use dictionaries and fix syntax errors --- examples/lines_bars_and_markers/bar_label_demo.py | 4 ++-- examples/lines_bars_and_markers/bar_stacked.py | 4 ++-- examples/lines_bars_and_markers/barchart.py | 4 ++-- 3 files changed, 6 insertions(+), 6 deletions(-) diff --git a/examples/lines_bars_and_markers/bar_label_demo.py b/examples/lines_bars_and_markers/bar_label_demo.py index 5eefe3300634..146b6934bb48 100644 --- a/examples/lines_bars_and_markers/bar_label_demo.py +++ b/examples/lines_bars_and_markers/bar_label_demo.py @@ -24,14 +24,14 @@ 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 fig, ax = plt.subplots() bottom = np.zeros(3) -for sex, sex_count in zip(sexes, sex_counts): +for sex, sex_count in sex_counts.items(): p = ax.bar(species, sex_count, width, label=sex, bottom=bottom) bottom += sex_count diff --git a/examples/lines_bars_and_markers/bar_stacked.py b/examples/lines_bars_and_markers/bar_stacked.py index 82cc5b2decd4..81ee305e7072 100644 --- a/examples/lines_bars_and_markers/bar_stacked.py +++ b/examples/lines_bars_and_markers/bar_stacked.py @@ -20,13 +20,13 @@ 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) -for boolean, weight_count in zip(booleans, weight_counts): +for boolean, weight_count in weight_counts.items(): p = ax.bar(species, weight_count, width, label=boolean, bottom=bottom) bottom += weight_count diff --git a/examples/lines_bars_and_markers/barchart.py b/examples/lines_bars_and_markers/barchart.py index 327958a47821..13007dc9ead7 100644 --- a/examples/lines_bars_and_markers/barchart.py +++ b/examples/lines_bars_and_markers/barchart.py @@ -24,9 +24,9 @@ multiplier = 0 fig, ax = plt.subplots(constrained_layout=True) -for measurement, penguin_mean in zip(measurements, penguin_means): +for attribute, measurement in penguin_means.items(): offset = width * multiplier - rects = ax.bar(x + offset, penguin_mean, width, label=measurement) + rects = ax.bar(x + offset, measurement, width, label=attribute) ax.bar_label(rects, padding=3) multiplier += 1 From 1cf307ea8599584b7ef2b5ab0ff747eb26798fa5 Mon Sep 17 00:00:00 2001 From: Kostya Farber <73378227+kostyafarber@users.noreply.github.com> Date: Fri, 9 Dec 2022 21:11:40 +0000 Subject: [PATCH 14/14] Update examples/lines_bars_and_markers/barchart.py Co-authored-by: hannah --- examples/lines_bars_and_markers/barchart.py | 1 + 1 file changed, 1 insertion(+) diff --git a/examples/lines_bars_and_markers/barchart.py b/examples/lines_bars_and_markers/barchart.py index 13007dc9ead7..f5ee6833a881 100644 --- a/examples/lines_bars_and_markers/barchart.py +++ b/examples/lines_bars_and_markers/barchart.py @@ -22,6 +22,7 @@ x = np.arange(len(species)) # the label locations width = 0.25 # the width of the bars multiplier = 0 + fig, ax = plt.subplots(constrained_layout=True) for attribute, measurement in penguin_means.items():