From 9af5a6a32e41156270e77739e6a856049cd3bb76 Mon Sep 17 00:00:00 2001 From: KheshavKumar Date: Sun, 3 Dec 2023 15:20:53 -0500 Subject: [PATCH 1/9] Added test for Axes.bar_label --- lib/matplotlib/tests/test_datetime.py | 43 +++++++++++++++++++++++++-- 1 file changed, 40 insertions(+), 3 deletions(-) diff --git a/lib/matplotlib/tests/test_datetime.py b/lib/matplotlib/tests/test_datetime.py index ca8e1cb90732..19eced318220 100644 --- a/lib/matplotlib/tests/test_datetime.py +++ b/lib/matplotlib/tests/test_datetime.py @@ -140,11 +140,48 @@ def test_bar(self): fig, ax = plt.subplots() ax.bar(...) - @pytest.mark.xfail(reason="Test for bar_label not written yet") @mpl.style.context("default") def test_bar_label(self): - fig, ax = plt.subplots() - ax.bar_label(...) + # Generate some example data with dateTime inputs + date_list = [datetime(2023, 1, 1) + timedelta(days=i) for i in range(5)] + values = [10, 20, 15, 25, 30] + + # Create a bar plot + fig, axs = plt.subplots(2, 2, figsize=(10, 8)) + fig.suptitle('Variations of ax.bar_label', fontsize=16) + + # Variation 1: Default settings + axs[0, 0].bar(date_list, values) + axs[0, 0].xaxis_date() + axs[0, 0].xaxis.set_major_formatter(mdates.DateFormatter('%Y-%m-%d')) + axs[0, 0].bar_label(axs[0, 0].containers[0]) # Using default settings + + # Variation 2: Label on top, with percentage formatting + axs[0, 1].bar(date_list, values) + axs[0, 1].xaxis_date() + axs[0, 1].xaxis.set_major_formatter(mdates.DateFormatter('%Y-%m-%d')) + axs[0, 1].bar_label(axs[0, 1].containers[0], fmt='%.1f%%', label_type='center', color='blue') + + # Variation 3: Label inside, with custom formatting + axs[1, 0].bar(date_list, values) + axs[1, 0].xaxis_date() + axs[1, 0].xaxis.set_major_formatter(mdates.DateFormatter('%Y-%m-%d')) + axs[1, 0].bar_label(axs[1, 0].containers[0], fmt='%d', label_type='center', color='white') + + # Variation 4: Label outside, with rotated text + axs[1, 1].bar(date_list, values) + axs[1, 1].xaxis_date() + axs[1, 1].xaxis.set_major_formatter(mdates.DateFormatter('%Y-%m-%d')) + axs[1, 1].bar_label(axs[1, 1].containers[0], fmt='%d', label_type='edge', color='red', rotation=45) + + # Adjust layout + plt.tight_layout(rect=[0, 0.03, 1, 0.95]) + + # Show the plot + plt.show() + + + @mpl.style.context("default") def test_barbs(self): From ee0490e35302df8e73eafecd3a3ded598f30dcb2 Mon Sep 17 00:00:00 2001 From: KheshavKumar Date: Sun, 3 Dec 2023 15:29:38 -0500 Subject: [PATCH 2/9] Added test for Axes.bar_label after fixing typos --- lib/matplotlib/tests/test_datetime.py | 18 ++++++++++++------ 1 file changed, 12 insertions(+), 6 deletions(-) diff --git a/lib/matplotlib/tests/test_datetime.py b/lib/matplotlib/tests/test_datetime.py index 19eced318220..74142378fc20 100644 --- a/lib/matplotlib/tests/test_datetime.py +++ b/lib/matplotlib/tests/test_datetime.py @@ -7,6 +7,7 @@ import matplotlib as mpl + class TestDatetimePlotting: @pytest.mark.xfail(reason="Test for acorr not written yet") @mpl.style.context("default") @@ -142,8 +143,13 @@ def test_bar(self): @mpl.style.context("default") def test_bar_label(self): + """ + import matplotlib.pyplot as plt + import matplotlib.dates as mdates + from datetime import datetime, timedelta + """ # Generate some example data with dateTime inputs - date_list = [datetime(2023, 1, 1) + timedelta(days=i) for i in range(5)] + date_list = [datetime.datetime(2023, 1, 1) + datetime.timedelta(days=i) for i in range(5)] values = [10, 20, 15, 25, 30] # Create a bar plot @@ -153,25 +159,25 @@ def test_bar_label(self): # Variation 1: Default settings axs[0, 0].bar(date_list, values) axs[0, 0].xaxis_date() - axs[0, 0].xaxis.set_major_formatter(mdates.DateFormatter('%Y-%m-%d')) + axs[0, 0].xaxis.set_major_formatter(mpl.dates.DateFormatter('%Y-%m-%d')) axs[0, 0].bar_label(axs[0, 0].containers[0]) # Using default settings # Variation 2: Label on top, with percentage formatting axs[0, 1].bar(date_list, values) axs[0, 1].xaxis_date() - axs[0, 1].xaxis.set_major_formatter(mdates.DateFormatter('%Y-%m-%d')) + axs[0, 1].xaxis.set_major_formatter(mpl.dates.DateFormatter('%Y-%m-%d')) axs[0, 1].bar_label(axs[0, 1].containers[0], fmt='%.1f%%', label_type='center', color='blue') # Variation 3: Label inside, with custom formatting axs[1, 0].bar(date_list, values) axs[1, 0].xaxis_date() - axs[1, 0].xaxis.set_major_formatter(mdates.DateFormatter('%Y-%m-%d')) + axs[1, 0].xaxis.set_major_formatter(mpl.dates.DateFormatter('%Y-%m-%d')) axs[1, 0].bar_label(axs[1, 0].containers[0], fmt='%d', label_type='center', color='white') # Variation 4: Label outside, with rotated text axs[1, 1].bar(date_list, values) axs[1, 1].xaxis_date() - axs[1, 1].xaxis.set_major_formatter(mdates.DateFormatter('%Y-%m-%d')) + axs[1, 1].xaxis.set_major_formatter(mpl.dates.DateFormatter('%Y-%m-%d')) axs[1, 1].bar_label(axs[1, 1].containers[0], fmt='%d', label_type='edge', color='red', rotation=45) # Adjust layout @@ -184,7 +190,7 @@ def test_bar_label(self): @mpl.style.context("default") - def test_barbs(self): + def test_barbs(self): plt.rcParams["date.converter"] = 'concise' start_date = datetime.datetime(2022, 2, 8, 22) From 37f36033cfed49d14ed147bc92caac8cb0c5cf83 Mon Sep 17 00:00:00 2001 From: KheshavKumar Date: Sun, 3 Dec 2023 15:45:00 -0500 Subject: [PATCH 3/9] Added test for Axes.bar_label after fixing typos --- lib/matplotlib/tests/test_datetime.py | 22 +++++++++------------- 1 file changed, 9 insertions(+), 13 deletions(-) diff --git a/lib/matplotlib/tests/test_datetime.py b/lib/matplotlib/tests/test_datetime.py index 74142378fc20..06b42a8d1745 100644 --- a/lib/matplotlib/tests/test_datetime.py +++ b/lib/matplotlib/tests/test_datetime.py @@ -7,7 +7,6 @@ import matplotlib as mpl - class TestDatetimePlotting: @pytest.mark.xfail(reason="Test for acorr not written yet") @mpl.style.context("default") @@ -143,13 +142,10 @@ def test_bar(self): @mpl.style.context("default") def test_bar_label(self): - """ - import matplotlib.pyplot as plt - import matplotlib.dates as mdates - from datetime import datetime, timedelta - """ + # Generate some example data with dateTime inputs - date_list = [datetime.datetime(2023, 1, 1) + datetime.timedelta(days=i) for i in range(5)] + date_list = [datetime.datetime(2023, 1, 1) + + datetime.timedelta(days=i) for i in range(5)] values = [10, 20, 15, 25, 30] # Create a bar plot @@ -166,19 +162,22 @@ def test_bar_label(self): axs[0, 1].bar(date_list, values) axs[0, 1].xaxis_date() axs[0, 1].xaxis.set_major_formatter(mpl.dates.DateFormatter('%Y-%m-%d')) - axs[0, 1].bar_label(axs[0, 1].containers[0], fmt='%.1f%%', label_type='center', color='blue') + axs[0, 1].bar_label(axs[0, 1].containers[0], + fmt='%.1f%%', label_type='center', color='blue') # Variation 3: Label inside, with custom formatting axs[1, 0].bar(date_list, values) axs[1, 0].xaxis_date() axs[1, 0].xaxis.set_major_formatter(mpl.dates.DateFormatter('%Y-%m-%d')) - axs[1, 0].bar_label(axs[1, 0].containers[0], fmt='%d', label_type='center', color='white') + axs[1, 0].bar_label(axs[1, 0].containers[0], fmt='%d', + label_type='center', color='white') # Variation 4: Label outside, with rotated text axs[1, 1].bar(date_list, values) axs[1, 1].xaxis_date() axs[1, 1].xaxis.set_major_formatter(mpl.dates.DateFormatter('%Y-%m-%d')) - axs[1, 1].bar_label(axs[1, 1].containers[0], fmt='%d', label_type='edge', color='red', rotation=45) + axs[1, 1].bar_label(axs[1, 1].containers[0], fmt='%d', + label_type='edge', color='red', rotation=45) # Adjust layout plt.tight_layout(rect=[0, 0.03, 1, 0.95]) @@ -186,9 +185,6 @@ def test_bar_label(self): # Show the plot plt.show() - - - @mpl.style.context("default") def test_barbs(self): plt.rcParams["date.converter"] = 'concise' From bc9910a55235c6587fae55b99bc02c2bc4dece4a Mon Sep 17 00:00:00 2001 From: KheshavKumar Date: Sun, 3 Dec 2023 15:48:07 -0500 Subject: [PATCH 4/9] Added test for Axes.bar_label after fixing typos --- lib/matplotlib/tests/test_datetime.py | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/lib/matplotlib/tests/test_datetime.py b/lib/matplotlib/tests/test_datetime.py index 06b42a8d1745..d3ec78ec6639 100644 --- a/lib/matplotlib/tests/test_datetime.py +++ b/lib/matplotlib/tests/test_datetime.py @@ -142,7 +142,6 @@ def test_bar(self): @mpl.style.context("default") def test_bar_label(self): - # Generate some example data with dateTime inputs date_list = [datetime.datetime(2023, 1, 1) + datetime.timedelta(days=i) for i in range(5)] @@ -186,7 +185,7 @@ def test_bar_label(self): plt.show() @mpl.style.context("default") - def test_barbs(self): + def test_barbs(self): plt.rcParams["date.converter"] = 'concise' start_date = datetime.datetime(2022, 2, 8, 22) From 0c7e5f8a8e4f7827f302000c7ed62e5cb38c1e44 Mon Sep 17 00:00:00 2001 From: KheshavKumar Date: Sun, 3 Dec 2023 19:55:43 -0500 Subject: [PATCH 5/9] Updated code based on review --- lib/matplotlib/tests/test_datetime.py | 41 ++++----------------------- 1 file changed, 5 insertions(+), 36 deletions(-) diff --git a/lib/matplotlib/tests/test_datetime.py b/lib/matplotlib/tests/test_datetime.py index d3ec78ec6639..444e0147c12b 100644 --- a/lib/matplotlib/tests/test_datetime.py +++ b/lib/matplotlib/tests/test_datetime.py @@ -144,44 +144,13 @@ def test_bar(self): def test_bar_label(self): # Generate some example data with dateTime inputs date_list = [datetime.datetime(2023, 1, 1) + - datetime.timedelta(days=i) for i in range(5)] + datetime.timedelta(days=i) for i in range(5)] values = [10, 20, 15, 25, 30] - # Create a bar plot - fig, axs = plt.subplots(2, 2, figsize=(10, 8)) - fig.suptitle('Variations of ax.bar_label', fontsize=16) - - # Variation 1: Default settings - axs[0, 0].bar(date_list, values) - axs[0, 0].xaxis_date() - axs[0, 0].xaxis.set_major_formatter(mpl.dates.DateFormatter('%Y-%m-%d')) - axs[0, 0].bar_label(axs[0, 0].containers[0]) # Using default settings - - # Variation 2: Label on top, with percentage formatting - axs[0, 1].bar(date_list, values) - axs[0, 1].xaxis_date() - axs[0, 1].xaxis.set_major_formatter(mpl.dates.DateFormatter('%Y-%m-%d')) - axs[0, 1].bar_label(axs[0, 1].containers[0], - fmt='%.1f%%', label_type='center', color='blue') - - # Variation 3: Label inside, with custom formatting - axs[1, 0].bar(date_list, values) - axs[1, 0].xaxis_date() - axs[1, 0].xaxis.set_major_formatter(mpl.dates.DateFormatter('%Y-%m-%d')) - axs[1, 0].bar_label(axs[1, 0].containers[0], fmt='%d', - label_type='center', color='white') - - # Variation 4: Label outside, with rotated text - axs[1, 1].bar(date_list, values) - axs[1, 1].xaxis_date() - axs[1, 1].xaxis.set_major_formatter(mpl.dates.DateFormatter('%Y-%m-%d')) - axs[1, 1].bar_label(axs[1, 1].containers[0], fmt='%d', - label_type='edge', color='red', rotation=45) - - # Adjust layout - plt.tight_layout(rect=[0, 0.03, 1, 0.95]) - - # Show the plot + # Creating the plot + fig, axs = plt.subplots(1,1, figsize=(10, 8), layout='constrained') + axs.bar(date_list, values) + plt.show() @mpl.style.context("default") From 2719ef473453ef083b61063f71c06bcfd25aef9e Mon Sep 17 00:00:00 2001 From: KheshavKumar <59756754+KheshavKumar@users.noreply.github.com> Date: Sun, 3 Dec 2023 20:11:41 -0500 Subject: [PATCH 6/9] Update test_datetime.py --- lib/matplotlib/tests/test_datetime.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/matplotlib/tests/test_datetime.py b/lib/matplotlib/tests/test_datetime.py index 444e0147c12b..46f446f8c5eb 100644 --- a/lib/matplotlib/tests/test_datetime.py +++ b/lib/matplotlib/tests/test_datetime.py @@ -144,11 +144,11 @@ def test_bar(self): def test_bar_label(self): # Generate some example data with dateTime inputs date_list = [datetime.datetime(2023, 1, 1) + - datetime.timedelta(days=i) for i in range(5)] + datetime.timedelta(days=i) for i in range(5)] values = [10, 20, 15, 25, 30] # Creating the plot - fig, axs = plt.subplots(1,1, figsize=(10, 8), layout='constrained') + fig, axs = plt.subplots(1, 1, figsize=(10, 8), layout='constrained') axs.bar(date_list, values) plt.show() From e2f416d376015465d07c6deb8c2d3bd96d7ce851 Mon Sep 17 00:00:00 2001 From: KheshavKumar Date: Mon, 4 Dec 2023 20:32:11 -0500 Subject: [PATCH 7/9] removed the .show line and added bar_label back --- lib/matplotlib/tests/test_datetime.py | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/lib/matplotlib/tests/test_datetime.py b/lib/matplotlib/tests/test_datetime.py index 444e0147c12b..b803b78511af 100644 --- a/lib/matplotlib/tests/test_datetime.py +++ b/lib/matplotlib/tests/test_datetime.py @@ -144,14 +144,15 @@ def test_bar(self): def test_bar_label(self): # Generate some example data with dateTime inputs date_list = [datetime.datetime(2023, 1, 1) + - datetime.timedelta(days=i) for i in range(5)] + datetime.timedelta(days=i) for i in range(5)] values = [10, 20, 15, 25, 30] # Creating the plot fig, axs = plt.subplots(1,1, figsize=(10, 8), layout='constrained') - axs.bar(date_list, values) + bars = axs.bar(date_list, values) - plt.show() + # Add labels to the bars using bar_label + axs.bar_label(bars, labels=[f'{val}%' for val in values], label_type='edge', color='black') @mpl.style.context("default") def test_barbs(self): From dfd1ee81f0b7539328e77f54d8902351fb11b5c5 Mon Sep 17 00:00:00 2001 From: KheshavKumar Date: Mon, 4 Dec 2023 20:39:39 -0500 Subject: [PATCH 8/9] added bar_label, removed show, fixed linting issues --- lib/matplotlib/tests/test_datetime.py | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/lib/matplotlib/tests/test_datetime.py b/lib/matplotlib/tests/test_datetime.py index b803b78511af..dff4ecd552bf 100644 --- a/lib/matplotlib/tests/test_datetime.py +++ b/lib/matplotlib/tests/test_datetime.py @@ -148,11 +148,12 @@ def test_bar_label(self): values = [10, 20, 15, 25, 30] # Creating the plot - fig, axs = plt.subplots(1,1, figsize=(10, 8), layout='constrained') + fig, axs = plt.subplots(1, 1, figsize=(10, 8), layout='constrained') bars = axs.bar(date_list, values) # Add labels to the bars using bar_label - axs.bar_label(bars, labels=[f'{val}%' for val in values], label_type='edge', color='black') + axs.bar_label(bars, labels=[f'{val}%' for val in values], + label_type='edge', color='black') @mpl.style.context("default") def test_barbs(self): From 1d5630e70393197596bf25fba657a605a47734f3 Mon Sep 17 00:00:00 2001 From: KheshavKumar <59756754+KheshavKumar@users.noreply.github.com> Date: Wed, 6 Dec 2023 20:24:37 -0500 Subject: [PATCH 9/9] Update lib/matplotlib/tests/test_datetime.py Changed axs to ax Co-authored-by: Elliott Sales de Andrade --- lib/matplotlib/tests/test_datetime.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/lib/matplotlib/tests/test_datetime.py b/lib/matplotlib/tests/test_datetime.py index dff4ecd552bf..9eb46bb32fe4 100644 --- a/lib/matplotlib/tests/test_datetime.py +++ b/lib/matplotlib/tests/test_datetime.py @@ -148,12 +148,12 @@ def test_bar_label(self): values = [10, 20, 15, 25, 30] # Creating the plot - fig, axs = plt.subplots(1, 1, figsize=(10, 8), layout='constrained') - bars = axs.bar(date_list, values) + fig, ax = plt.subplots(1, 1, figsize=(10, 8), layout='constrained') + bars = ax.bar(date_list, values) # Add labels to the bars using bar_label - axs.bar_label(bars, labels=[f'{val}%' for val in values], - label_type='edge', color='black') + ax.bar_label(bars, labels=[f'{val}%' for val in values], + label_type='edge', color='black') @mpl.style.context("default") def test_barbs(self):