From 15571fc7d9a244d53e452805b25bea7721fecfad Mon Sep 17 00:00:00 2001 From: parthpankajtiwary Date: Sun, 27 Nov 2022 19:36:59 +0100 Subject: [PATCH 1/4] ENH: Add warning for SymLogScale when values in linear scale --- lib/matplotlib/scale.py | 5 +++++ lib/matplotlib/tests/test_scale.py | 21 +++++++++++++++++++++ 2 files changed, 26 insertions(+) diff --git a/lib/matplotlib/scale.py b/lib/matplotlib/scale.py index 21099c8594d5..534165cc72e5 100644 --- a/lib/matplotlib/scale.py +++ b/lib/matplotlib/scale.py @@ -389,6 +389,11 @@ def __init__(self, base, linthresh, linscale): def transform_non_affine(self, a): abs_a = np.abs(a) + if (abs_a < self.linthresh).all(): + _api.warn_external( + "All values for SymLogScale are below linthresh, making " + "it effectively linear. You likely should lower the value " + "of linthresh. ") with np.errstate(divide="ignore", invalid="ignore"): out = np.sign(a) * self.linthresh * ( np.power(self.base, diff --git a/lib/matplotlib/tests/test_scale.py b/lib/matplotlib/tests/test_scale.py index 7f1130560581..e0713fe84c4b 100644 --- a/lib/matplotlib/tests/test_scale.py +++ b/lib/matplotlib/tests/test_scale.py @@ -14,6 +14,8 @@ import io import pytest +import random + @check_figures_equal() def test_log_scales(fig_test, fig_ref): @@ -55,6 +57,25 @@ def test_symlog_mask_nan(): assert type(out) == type(x) +def test_symlog_linthresh(): + fig, ax = plt.subplots() + + n_samples = 100 + + upper_bound = 1.0 + + x = [random.uniform(0.0, upper_bound) for _ in range(n_samples)] + y = [random.uniform(0.0, upper_bound) for _ in range(n_samples)] + + with pytest.warns(UserWarning) as record: + plt.plot(x, y, 'o') + ax.set_xscale('symlog') + ax.set_yscale('symlog') + plt.show() + + assert len(record) == 1 + + @image_comparison(['logit_scales.png'], remove_text=True) def test_logit_scales(): fig, ax = plt.subplots() From bd6a88ed4a35867580c16ff546a76d7612608e31 Mon Sep 17 00:00:00 2001 From: parthpankajtiwary Date: Mon, 28 Nov 2022 10:09:29 +0100 Subject: [PATCH 2/4] Update unittest attune to numpy standards --- lib/matplotlib/tests/test_scale.py | 20 +++++++------------- 1 file changed, 7 insertions(+), 13 deletions(-) diff --git a/lib/matplotlib/tests/test_scale.py b/lib/matplotlib/tests/test_scale.py index e0713fe84c4b..d10956e4a6d2 100644 --- a/lib/matplotlib/tests/test_scale.py +++ b/lib/matplotlib/tests/test_scale.py @@ -14,8 +14,6 @@ import io import pytest -import random - @check_figures_equal() def test_log_scales(fig_test, fig_ref): @@ -60,21 +58,17 @@ def test_symlog_mask_nan(): def test_symlog_linthresh(): fig, ax = plt.subplots() - n_samples = 100 - - upper_bound = 1.0 + np.random.seed(19680801) + x = np.random.random(100) + y = np.random.random(100) - x = [random.uniform(0.0, upper_bound) for _ in range(n_samples)] - y = [random.uniform(0.0, upper_bound) for _ in range(n_samples)] + plt.plot(x, y, 'o') + ax.set_xscale('symlog') + ax.set_yscale('symlog') - with pytest.warns(UserWarning) as record: - plt.plot(x, y, 'o') - ax.set_xscale('symlog') - ax.set_yscale('symlog') + with pytest.warns(UserWarning): plt.show() - assert len(record) == 1 - @image_comparison(['logit_scales.png'], remove_text=True) def test_logit_scales(): From 10c413adfcdd5e1f01596c04748eae5bb2b8f0f8 Mon Sep 17 00:00:00 2001 From: parthpankajtiwary Date: Mon, 28 Nov 2022 10:48:32 +0100 Subject: [PATCH 3/4] Add force draw to generate the warning --- lib/matplotlib/tests/test_scale.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/matplotlib/tests/test_scale.py b/lib/matplotlib/tests/test_scale.py index d10956e4a6d2..0e22da773714 100644 --- a/lib/matplotlib/tests/test_scale.py +++ b/lib/matplotlib/tests/test_scale.py @@ -66,8 +66,8 @@ def test_symlog_linthresh(): ax.set_xscale('symlog') ax.set_yscale('symlog') - with pytest.warns(UserWarning): - plt.show() + with pytest.warns(UserWarning, match="All values .* of linthresh"): + fig.canvas.draw() @image_comparison(['logit_scales.png'], remove_text=True) From 6bc9f6152c5f52ff99b92bd1071ea42e54924ff9 Mon Sep 17 00:00:00 2001 From: cargobuild Date: Mon, 28 Nov 2022 14:14:27 +0100 Subject: [PATCH 4/4] Refactor test_symlog_linthresh unit test Co-authored-by: Tim Hoffmann <2836374+timhoffm@users.noreply.github.com> --- lib/matplotlib/tests/test_scale.py | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/lib/matplotlib/tests/test_scale.py b/lib/matplotlib/tests/test_scale.py index 0e22da773714..3b70d1e9d31d 100644 --- a/lib/matplotlib/tests/test_scale.py +++ b/lib/matplotlib/tests/test_scale.py @@ -56,12 +56,11 @@ def test_symlog_mask_nan(): def test_symlog_linthresh(): - fig, ax = plt.subplots() - np.random.seed(19680801) x = np.random.random(100) y = np.random.random(100) + fig, ax = plt.subplots() plt.plot(x, y, 'o') ax.set_xscale('symlog') ax.set_yscale('symlog')