From 4ab5da8eb203a1da8237c6a44c01822044af4be9 Mon Sep 17 00:00:00 2001 From: Ali Aboelela Date: Sat, 9 Dec 2023 20:23:00 -0500 Subject: [PATCH 1/6] Defined the test_clabel function in the test_datetime file --- lib/matplotlib/tests/test_datetime.py | 19 +++++++++++++++++-- 1 file changed, 17 insertions(+), 2 deletions(-) diff --git a/lib/matplotlib/tests/test_datetime.py b/lib/matplotlib/tests/test_datetime.py index 3c8cc8b5b8bb..d96682a655f9 100644 --- a/lib/matplotlib/tests/test_datetime.py +++ b/lib/matplotlib/tests/test_datetime.py @@ -259,11 +259,26 @@ def test_bxp(self): ax.xaxis.set_major_formatter(mpl.dates.DateFormatter("%Y-%m-%d")) ax.set_title('Box plot with datetime data') - @pytest.mark.xfail(reason="Test for clabel not written yet") @mpl.style.context("default") def test_clabel(self): + # Sample data for contour plot + x_start, x_end, x_step = -10.0, 5.0, 0.5 + y_start, y_end, y_step = -10.0, 5.0, 0.5 + + x = np.arange(x_start, x_end, x_step) + y = np.arange(y_start, y_end, y_step) + + X, Y = np.meshgrid(x, y) + Z = np.sqrt(X**2 + Y**2) + fig, ax = plt.subplots() - ax.clabel(...) + CS = ax.contour(X, Y, Z) + + # Input date object to be used as test + dates = [datetime.datetime(2023, 10, 1) + datetime.timedelta(days=i) for i in range(len(CS.levels))] + + ax.clabel(CS, CS.levels, inline=True, fmt=dict(zip(CS.levels, dates))) + plt.show() @pytest.mark.xfail(reason="Test for contour not written yet") @mpl.style.context("default") From 6aec0e976c22107d4a3b7cdf0c3467587face894 Mon Sep 17 00:00:00 2001 From: Ali Aboelela Date: Sat, 9 Dec 2023 20:38:35 -0500 Subject: [PATCH 2/6] Removed plt.show() line from test --- lib/matplotlib/tests/test_datetime.py | 1 - 1 file changed, 1 deletion(-) diff --git a/lib/matplotlib/tests/test_datetime.py b/lib/matplotlib/tests/test_datetime.py index d96682a655f9..0aaa8ced23b3 100644 --- a/lib/matplotlib/tests/test_datetime.py +++ b/lib/matplotlib/tests/test_datetime.py @@ -278,7 +278,6 @@ def test_clabel(self): dates = [datetime.datetime(2023, 10, 1) + datetime.timedelta(days=i) for i in range(len(CS.levels))] ax.clabel(CS, CS.levels, inline=True, fmt=dict(zip(CS.levels, dates))) - plt.show() @pytest.mark.xfail(reason="Test for contour not written yet") @mpl.style.context("default") From a1de1ab9243d0b920052b641abbbaa9dcc13466d Mon Sep 17 00:00:00 2001 From: Ali Aboelela Date: Sat, 9 Dec 2023 20:41:31 -0500 Subject: [PATCH 3/6] Shortened line length to pass lint check --- lib/matplotlib/tests/test_datetime.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/lib/matplotlib/tests/test_datetime.py b/lib/matplotlib/tests/test_datetime.py index 0aaa8ced23b3..3d231038f884 100644 --- a/lib/matplotlib/tests/test_datetime.py +++ b/lib/matplotlib/tests/test_datetime.py @@ -275,7 +275,8 @@ def test_clabel(self): CS = ax.contour(X, Y, Z) # Input date object to be used as test - dates = [datetime.datetime(2023, 10, 1) + datetime.timedelta(days=i) for i in range(len(CS.levels))] + dates = [datetime.datetime(2023, 10, 1) + datetime.timedelta(days=i) + for i in range(len(CS.levels))] ax.clabel(CS, CS.levels, inline=True, fmt=dict(zip(CS.levels, dates))) From eee31b5a345ff668b454bfb4e6086a1d43f38020 Mon Sep 17 00:00:00 2001 From: Ali Aboelela Date: Sun, 10 Dec 2023 08:01:36 -0500 Subject: [PATCH 4/6] Set x-axis of contour plot to datetime --- lib/matplotlib/tests/test_datetime.py | 18 ++++++++++++++++-- 1 file changed, 16 insertions(+), 2 deletions(-) diff --git a/lib/matplotlib/tests/test_datetime.py b/lib/matplotlib/tests/test_datetime.py index 3d231038f884..c2f3b614d012 100644 --- a/lib/matplotlib/tests/test_datetime.py +++ b/lib/matplotlib/tests/test_datetime.py @@ -1,6 +1,6 @@ import datetime +import math import numpy as np - import pytest import matplotlib.pyplot as plt @@ -265,9 +265,9 @@ def test_clabel(self): x_start, x_end, x_step = -10.0, 5.0, 0.5 y_start, y_end, y_step = -10.0, 5.0, 0.5 + # Trying to generate a contour using dates will either x = np.arange(x_start, x_end, x_step) y = np.arange(y_start, y_end, y_step) - X, Y = np.meshgrid(x, y) Z = np.sqrt(X**2 + Y**2) @@ -278,7 +278,21 @@ def test_clabel(self): dates = [datetime.datetime(2023, 10, 1) + datetime.timedelta(days=i) for i in range(len(CS.levels))] + # Passing dates to label the contours directly works as expected ax.clabel(CS, CS.levels, inline=True, fmt=dict(zip(CS.levels, dates))) + xinterval = math.floor(len(x)/len(dates)) + 3 + numXlabels = math.floor(len(x)/xinterval) + + ax.set_xticks(x[::xinterval]) # Set ticks at regular intervals + str_dates = [] + + for i in range(numXlabels): + str_dates.append(dates[i].strftime('%Y-%m-%d')) + + # Works, but not readable + #labels = dates[:numXlabels:] + labels = str_dates + ax.set_xticklabels(labels) # Format labels as dates @pytest.mark.xfail(reason="Test for contour not written yet") @mpl.style.context("default") From 85bfc3917dc1c9926866a9c609f067f8fdda70e5 Mon Sep 17 00:00:00 2001 From: Ali Aboelela Date: Sun, 10 Dec 2023 08:06:00 -0500 Subject: [PATCH 5/6] Fixed a bad comment --- lib/matplotlib/tests/test_datetime.py | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/lib/matplotlib/tests/test_datetime.py b/lib/matplotlib/tests/test_datetime.py index c2f3b614d012..a261a2d0feaf 100644 --- a/lib/matplotlib/tests/test_datetime.py +++ b/lib/matplotlib/tests/test_datetime.py @@ -265,7 +265,8 @@ def test_clabel(self): x_start, x_end, x_step = -10.0, 5.0, 0.5 y_start, y_end, y_step = -10.0, 5.0, 0.5 - # Trying to generate a contour using dates will either + # Trying to generate a contour using dates will either fail when you + # do math or fail when generating the contour x = np.arange(x_start, x_end, x_step) y = np.arange(y_start, y_end, y_step) X, Y = np.meshgrid(x, y) @@ -280,17 +281,18 @@ def test_clabel(self): # Passing dates to label the contours directly works as expected ax.clabel(CS, CS.levels, inline=True, fmt=dict(zip(CS.levels, dates))) + + # Set ticks at regular intervals to manually set x axis via dates xinterval = math.floor(len(x)/len(dates)) + 3 numXlabels = math.floor(len(x)/xinterval) + ax.set_xticks(x[::xinterval]) - ax.set_xticks(x[::xinterval]) # Set ticks at regular intervals + # Works, but not readable + #labels = dates[:numXlabels:] str_dates = [] - for i in range(numXlabels): str_dates.append(dates[i].strftime('%Y-%m-%d')) - - # Works, but not readable - #labels = dates[:numXlabels:] + labels = str_dates ax.set_xticklabels(labels) # Format labels as dates From 37df53cc96d2ac7d9c9c3201e195c47a5f6f655e Mon Sep 17 00:00:00 2001 From: Ali Aboelela Date: Sun, 10 Dec 2023 12:03:33 -0500 Subject: [PATCH 6/6] Modified test so it's testing intended functionality i.e. passing datetime objects directly to contour when creating contour plot using clabel --- lib/matplotlib/tests/test_datetime.py | 40 +++++++++++---------------- 1 file changed, 16 insertions(+), 24 deletions(-) diff --git a/lib/matplotlib/tests/test_datetime.py b/lib/matplotlib/tests/test_datetime.py index a261a2d0feaf..0656f4c7cf48 100644 --- a/lib/matplotlib/tests/test_datetime.py +++ b/lib/matplotlib/tests/test_datetime.py @@ -262,39 +262,31 @@ def test_bxp(self): @mpl.style.context("default") def test_clabel(self): # Sample data for contour plot + dates = [datetime.datetime(2023, 10, 1) + datetime.timedelta(days=i) + for i in range(10)] + x_start, x_end, x_step = -10.0, 5.0, 0.5 - y_start, y_end, y_step = -10.0, 5.0, 0.5 + y_start, y_end, y_step = 0, 10, 1 - # Trying to generate a contour using dates will either fail when you - # do math or fail when generating the contour x = np.arange(x_start, x_end, x_step) y = np.arange(y_start, y_end, y_step) - X, Y = np.meshgrid(x, y) - Z = np.sqrt(X**2 + Y**2) + + # In this case, Y axis has dates + X, Y = np.meshgrid(x, dates) + + # In this case, X axis has dates + #X, Y = np.meshgrid(dates, y) + + rows = len(X) + cols = len(X[0]) + + z1D = np.arange(rows * cols) + Z = z1D.reshape((rows, cols)) fig, ax = plt.subplots() CS = ax.contour(X, Y, Z) - # Input date object to be used as test - dates = [datetime.datetime(2023, 10, 1) + datetime.timedelta(days=i) - for i in range(len(CS.levels))] - - # Passing dates to label the contours directly works as expected ax.clabel(CS, CS.levels, inline=True, fmt=dict(zip(CS.levels, dates))) - - # Set ticks at regular intervals to manually set x axis via dates - xinterval = math.floor(len(x)/len(dates)) + 3 - numXlabels = math.floor(len(x)/xinterval) - ax.set_xticks(x[::xinterval]) - - # Works, but not readable - #labels = dates[:numXlabels:] - str_dates = [] - for i in range(numXlabels): - str_dates.append(dates[i].strftime('%Y-%m-%d')) - - labels = str_dates - ax.set_xticklabels(labels) # Format labels as dates @pytest.mark.xfail(reason="Test for contour not written yet") @mpl.style.context("default")