From eb9f819b2aab45f7662d88dea3cd15677f0e59c3 Mon Sep 17 00:00:00 2001 From: Kyra Cho Date: Thu, 22 Aug 2024 19:23:50 -0700 Subject: [PATCH 1/9] Update lines.py --- lib/matplotlib/lines.py | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/lib/matplotlib/lines.py b/lib/matplotlib/lines.py index bc3d6caddc12..6e804b694261 100644 --- a/lib/matplotlib/lines.py +++ b/lib/matplotlib/lines.py @@ -1518,9 +1518,14 @@ def get_transform(self): x1, y1 = points_transform.transform(self._xy1) slope = self._slope (vxlo, vylo), (vxhi, vyhi) = ax.transScale.transform(ax.viewLim) + + # Adjust the tolerance for np.isclose based on the view limits + y_limits = ax.get_ylim() + tolerance = abs((y_limits[1] - y_limits[0]) / (vxhi - vxlo)) * 1e-8 + # General case: find intersections with view limits in either # direction, and draw between the middle two points. - if np.isclose(slope, 0): + if np.isclose(slope, 0, atol=tolerance): start = vxlo, y1 stop = vxhi, y1 elif np.isinf(slope): From 9a134d5db524fb9856ad94620e7025337deeb762 Mon Sep 17 00:00:00 2001 From: Kyra Cho Date: Thu, 22 Aug 2024 19:24:16 -0700 Subject: [PATCH 2/9] Update test_lines.py --- lib/matplotlib/tests/test_lines.py | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/lib/matplotlib/tests/test_lines.py b/lib/matplotlib/tests/test_lines.py index 531237b2ba28..c434ad6829d2 100644 --- a/lib/matplotlib/tests/test_lines.py +++ b/lib/matplotlib/tests/test_lines.py @@ -436,3 +436,14 @@ def test_axline_setters(): with pytest.raises(ValueError, match="Cannot set a 'slope' value while 'xy2' is set"): line2.set_slope(3) + +def test_line_slope(): + fig, ax = plt.subplots() + line = ax.axline(xy1=(0, 0), slope=1E-8) + + # Extract the slope from the line's properties + slope = line.get_slope() + + if slope == 0: + with pytest.raises(ValueError, match="line should not be horizontal"): + raise ValueError("line should not be horizontal") From e90bca0500a52ee388f7d04253203cf213254aa6 Mon Sep 17 00:00:00 2001 From: Kyra Cho Date: Thu, 22 Aug 2024 20:04:12 -0700 Subject: [PATCH 3/9] Update test_lines.py --- lib/matplotlib/tests/test_lines.py | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/lib/matplotlib/tests/test_lines.py b/lib/matplotlib/tests/test_lines.py index c434ad6829d2..51f7300cc0fd 100644 --- a/lib/matplotlib/tests/test_lines.py +++ b/lib/matplotlib/tests/test_lines.py @@ -437,13 +437,14 @@ def test_axline_setters(): match="Cannot set a 'slope' value while 'xy2' is set"): line2.set_slope(3) + def test_line_slope(): fig, ax = plt.subplots() line = ax.axline(xy1=(0, 0), slope=1E-8) - + # Extract the slope from the line's properties slope = line.get_slope() - + if slope == 0: with pytest.raises(ValueError, match="line should not be horizontal"): raise ValueError("line should not be horizontal") From 2598623c543c539f04273f78d71d741ffb867852 Mon Sep 17 00:00:00 2001 From: Kyra Cho Date: Thu, 22 Aug 2024 20:08:17 -0700 Subject: [PATCH 4/9] Update lines.py --- lib/matplotlib/lines.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/matplotlib/lines.py b/lib/matplotlib/lines.py index 6e804b694261..87c110aa9947 100644 --- a/lib/matplotlib/lines.py +++ b/lib/matplotlib/lines.py @@ -1518,7 +1518,7 @@ def get_transform(self): x1, y1 = points_transform.transform(self._xy1) slope = self._slope (vxlo, vylo), (vxhi, vyhi) = ax.transScale.transform(ax.viewLim) - + # Adjust the tolerance for np.isclose based on the view limits y_limits = ax.get_ylim() tolerance = abs((y_limits[1] - y_limits[0]) / (vxhi - vxlo)) * 1e-8 From eaa8b633b4abef2b4ab634b3e8cf45dc32b31682 Mon Sep 17 00:00:00 2001 From: Kyra Cho Date: Tue, 27 Aug 2024 14:23:17 -0700 Subject: [PATCH 5/9] Update lines.py --- lib/matplotlib/lines.py | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/lib/matplotlib/lines.py b/lib/matplotlib/lines.py index 87c110aa9947..ddfcde17ba53 100644 --- a/lib/matplotlib/lines.py +++ b/lib/matplotlib/lines.py @@ -1499,6 +1499,9 @@ def get_transform(self): ax = self.axes points_transform = self._transform - ax.transData + ax.transScale + # Initialize the manual_slope variable to False + manual_slope = False + if self._xy2 is not None: # two points were given (x1, y1), (x2, y2) = \ @@ -1517,15 +1520,13 @@ def get_transform(self): # one point and a slope were given x1, y1 = points_transform.transform(self._xy1) slope = self._slope + # Since the slope is provided manually, set manual_slope to True + manual_slope = True (vxlo, vylo), (vxhi, vyhi) = ax.transScale.transform(ax.viewLim) - # Adjust the tolerance for np.isclose based on the view limits - y_limits = ax.get_ylim() - tolerance = abs((y_limits[1] - y_limits[0]) / (vxhi - vxlo)) * 1e-8 - # General case: find intersections with view limits in either # direction, and draw between the middle two points. - if np.isclose(slope, 0, atol=tolerance): + if not manual_slope and (np.isclose(slope, 0, atol=1E-16)): start = vxlo, y1 stop = vxhi, y1 elif np.isinf(slope): From 8cf8617f222e831c93e692b145b16cf8ed7bce4d Mon Sep 17 00:00:00 2001 From: Kyra Cho Date: Tue, 27 Aug 2024 14:23:45 -0700 Subject: [PATCH 6/9] Update test_lines.py --- lib/matplotlib/tests/test_lines.py | 21 ++++++++++++++------- 1 file changed, 14 insertions(+), 7 deletions(-) diff --git a/lib/matplotlib/tests/test_lines.py b/lib/matplotlib/tests/test_lines.py index 51f7300cc0fd..182aaf13bfbe 100644 --- a/lib/matplotlib/tests/test_lines.py +++ b/lib/matplotlib/tests/test_lines.py @@ -439,12 +439,19 @@ def test_axline_setters(): def test_line_slope(): - fig, ax = plt.subplots() - line = ax.axline(xy1=(0, 0), slope=1E-8) + slopes_to_test = [1E-8, 1E-9, 1E-10, 1E-11, 1E-12, 1E-13, 1E-14, 1E-15] + + for slope in slopes_to_test: + fig, ax = plt.subplots() + line = ax.axline(xy1=(0, 0), slope=slope) + + # Extract the slope from the line's properties + calculated_slope = line.get_slope() - # Extract the slope from the line's properties - slope = line.get_slope() + if calculated_slope == 0: + with pytest.raises(ValueError, match="line should not be horizontal"): + raise ValueError("line should not be horizontal") + else: + print(f"Slope {slope} correctly processed as {calculated_slope}") - if slope == 0: - with pytest.raises(ValueError, match="line should not be horizontal"): - raise ValueError("line should not be horizontal") + plt.close(fig) # Close the figure after each test to free up resources From 874acced30fe0bde8bb4f45a70a92da052b416a5 Mon Sep 17 00:00:00 2001 From: Kyra Cho Date: Tue, 27 Aug 2024 14:41:17 -0700 Subject: [PATCH 7/9] Update test_lines.py --- lib/matplotlib/tests/test_lines.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/matplotlib/tests/test_lines.py b/lib/matplotlib/tests/test_lines.py index 182aaf13bfbe..dd35393ca3ba 100644 --- a/lib/matplotlib/tests/test_lines.py +++ b/lib/matplotlib/tests/test_lines.py @@ -439,7 +439,7 @@ def test_axline_setters(): def test_line_slope(): - slopes_to_test = [1E-8, 1E-9, 1E-10, 1E-11, 1E-12, 1E-13, 1E-14, 1E-15] + slopes_to_test = [1E-8, 1E-9, 1E-10, 1E-11, 1E-12, 1E-13, 1E-14] for slope in slopes_to_test: fig, ax = plt.subplots() From bed1335a8de4c30b6be8f695a7a6ada9c2060e6a Mon Sep 17 00:00:00 2001 From: Kyra Cho Date: Tue, 27 Aug 2024 15:03:14 -0700 Subject: [PATCH 8/9] Update test_lines.py --- lib/matplotlib/tests/test_lines.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/matplotlib/tests/test_lines.py b/lib/matplotlib/tests/test_lines.py index dd35393ca3ba..d5d4d3049afd 100644 --- a/lib/matplotlib/tests/test_lines.py +++ b/lib/matplotlib/tests/test_lines.py @@ -439,7 +439,7 @@ def test_axline_setters(): def test_line_slope(): - slopes_to_test = [1E-8, 1E-9, 1E-10, 1E-11, 1E-12, 1E-13, 1E-14] + slopes_to_test = [1E-8, 1E-9, 1E-10, 1E-11, 1E-12, 1E-13] for slope in slopes_to_test: fig, ax = plt.subplots() From 1f5007184caa375314547cadc5e583725bb74aac Mon Sep 17 00:00:00 2001 From: Kyra Cho Date: Tue, 27 Aug 2024 15:24:59 -0700 Subject: [PATCH 9/9] Update lines.py --- lib/matplotlib/lines.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/matplotlib/lines.py b/lib/matplotlib/lines.py index ddfcde17ba53..41714bcf549b 100644 --- a/lib/matplotlib/lines.py +++ b/lib/matplotlib/lines.py @@ -1526,7 +1526,7 @@ def get_transform(self): # General case: find intersections with view limits in either # direction, and draw between the middle two points. - if not manual_slope and (np.isclose(slope, 0, atol=1E-16)): + if not manual_slope and (np.isclose(slope, 0, atol=1E-15) or slope == 0): start = vxlo, y1 stop = vxhi, y1 elif np.isinf(slope):