From 994abce7bff31372b4e25f5c0e6f40836d9c1727 Mon Sep 17 00:00:00 2001 From: danielcobej Date: Wed, 18 Oct 2023 15:14:12 +0200 Subject: [PATCH 1/3] added test_axhspan --- lib/matplotlib/tests/test_datetime.py | 32 ++++++++++++++++++++++++--- 1 file changed, 29 insertions(+), 3 deletions(-) diff --git a/lib/matplotlib/tests/test_datetime.py b/lib/matplotlib/tests/test_datetime.py index 276b7c179afe..3c5f664d7fdb 100644 --- a/lib/matplotlib/tests/test_datetime.py +++ b/lib/matplotlib/tests/test_datetime.py @@ -32,11 +32,37 @@ def test_axhline(self): fig, ax = plt.subplots() ax.axhline(...) - @pytest.mark.xfail(reason="Test for axhspan not written yet") @mpl.style.context("default") def test_axhspan(self): - fig, ax = plt.subplots() - ax.axhspan(...) + mpl.rcParams["date.converter"] = 'concise' + np.random.seed(19680801) + + start_date = datetime.datetime(2023, 1, 1) + time_delta = datetime.timedelta(days=1) + + values = np.random.randint(1, 10, 30) + bin_edges = [start_date + i * time_delta for i in range(31)] + + fig, (ax1, ax2) = plt.subplots(2, 1, constrained_layout=True) + + ax1.hist( + [start_date + i * time_delta for i in range(30)], + bins=bin_edges, + weights=values) + + for i in range(np.random.randint(1, 5)): + ymin = np.random.randint(1, 8) + ymax = ymin + np.random.randint(1, 3) + ax1.axhspan(ymin=ymin, ymax=ymax, facecolor='green', alpha=0.5) + + ax2.hist( + [start_date + i * time_delta for i in range(30)], + bins=bin_edges, + weights=values) + + y_values = np.unique(values) + for i, y in enumerate(y_values[::2]): + ax2.axhspan(ymin=y, ymax=y+1, facecolor='green', alpha=0.5) @pytest.mark.xfail(reason="Test for axline not written yet") @mpl.style.context("default") From 42c4ef04cdf64fc0618bcb9d1cd30d11fe0e5dbd Mon Sep 17 00:00:00 2001 From: danielcobej Date: Wed, 18 Oct 2023 23:09:25 +0200 Subject: [PATCH 2/3] switched x and y axis --- lib/matplotlib/tests/test_datetime.py | 28 +++++++++------------------ 1 file changed, 9 insertions(+), 19 deletions(-) diff --git a/lib/matplotlib/tests/test_datetime.py b/lib/matplotlib/tests/test_datetime.py index 3c5f664d7fdb..9a359d6c33b7 100644 --- a/lib/matplotlib/tests/test_datetime.py +++ b/lib/matplotlib/tests/test_datetime.py @@ -40,29 +40,19 @@ def test_axhspan(self): start_date = datetime.datetime(2023, 1, 1) time_delta = datetime.timedelta(days=1) - values = np.random.randint(1, 10, 30) - bin_edges = [start_date + i * time_delta for i in range(31)] - - fig, (ax1, ax2) = plt.subplots(2, 1, constrained_layout=True) - - ax1.hist( - [start_date + i * time_delta for i in range(30)], - bins=bin_edges, - weights=values) + fig, (ax1, ax2) = plt.subplots(2, 1, constrained_layout=True, figsize=(10, 8)) + ax1.set_ylim(start_date, start_date + 29*time_delta) for i in range(np.random.randint(1, 5)): - ymin = np.random.randint(1, 8) - ymax = ymin + np.random.randint(1, 3) + ymin = start_date + np.random.randint(0, 30) * time_delta + ymax = ymin + np.random.randint(1, 3) * time_delta ax1.axhspan(ymin=ymin, ymax=ymax, facecolor='green', alpha=0.5) - ax2.hist( - [start_date + i * time_delta for i in range(30)], - bins=bin_edges, - weights=values) - - y_values = np.unique(values) - for i, y in enumerate(y_values[::2]): - ax2.axhspan(ymin=y, ymax=y+1, facecolor='green', alpha=0.5) + ax2.set_ylim(start_date, start_date + 29*time_delta) + for i in range(0, 30, 2): + ymin = start_date + i * time_delta + ymax = ymin + time_delta + ax2.axhspan(ymin=ymin, ymax=ymax, facecolor='green', alpha=0.5) @pytest.mark.xfail(reason="Test for axline not written yet") @mpl.style.context("default") From 4af86972fafe0a9ebf18ddded200e910d25c57d2 Mon Sep 17 00:00:00 2001 From: danielcobej Date: Fri, 20 Oct 2023 15:48:24 +0200 Subject: [PATCH 3/3] datetime vs number, number vs datetime, datetime vs datetime --- lib/matplotlib/tests/test_datetime.py | 45 ++++++++++++++++++--------- 1 file changed, 30 insertions(+), 15 deletions(-) diff --git a/lib/matplotlib/tests/test_datetime.py b/lib/matplotlib/tests/test_datetime.py index 9a359d6c33b7..ca8b746bad0d 100644 --- a/lib/matplotlib/tests/test_datetime.py +++ b/lib/matplotlib/tests/test_datetime.py @@ -35,24 +35,39 @@ def test_axhline(self): @mpl.style.context("default") def test_axhspan(self): mpl.rcParams["date.converter"] = 'concise' - np.random.seed(19680801) start_date = datetime.datetime(2023, 1, 1) - time_delta = datetime.timedelta(days=1) - - fig, (ax1, ax2) = plt.subplots(2, 1, constrained_layout=True, figsize=(10, 8)) - - ax1.set_ylim(start_date, start_date + 29*time_delta) - for i in range(np.random.randint(1, 5)): - ymin = start_date + np.random.randint(0, 30) * time_delta - ymax = ymin + np.random.randint(1, 3) * time_delta - ax1.axhspan(ymin=ymin, ymax=ymax, facecolor='green', alpha=0.5) - - ax2.set_ylim(start_date, start_date + 29*time_delta) - for i in range(0, 30, 2): - ymin = start_date + i * time_delta - ymax = ymin + time_delta + dates = [start_date + datetime.timedelta(days=i) for i in range(31)] + numbers = list(range(1, 32)) + + fig, (ax1, ax2, ax3) = plt.subplots(3, 1, + constrained_layout=True, + figsize=(10, 12)) + + ax1.plot(dates, numbers, marker='o', color='blue') + for i in range(0, 31, 2): + ax1.axhspan(ymin=i+1, ymax=i+2, facecolor='green', alpha=0.5) + ax1.set_title('Datetime vs. Number') + ax1.set_xlabel('Date') + ax1.set_ylabel('Number') + + ax2.plot(numbers, dates, marker='o', color='blue') + for i in range(0, 31, 2): + ymin = start_date + datetime.timedelta(days=i) + ymax = ymin + datetime.timedelta(days=1) ax2.axhspan(ymin=ymin, ymax=ymax, facecolor='green', alpha=0.5) + ax2.set_title('Number vs. Datetime') + ax2.set_xlabel('Number') + ax2.set_ylabel('Date') + + ax3.plot(dates, dates, marker='o', color='blue') + for i in range(0, 31, 2): + ymin = start_date + datetime.timedelta(days=i) + ymax = ymin + datetime.timedelta(days=1) + ax3.axhspan(ymin=ymin, ymax=ymax, facecolor='green', alpha=0.5) + ax3.set_title('Datetime vs. Datetime') + ax3.set_xlabel('Date') + ax3.set_ylabel('Date') @pytest.mark.xfail(reason="Test for axline not written yet") @mpl.style.context("default")