From 5ee4bdda2cdac0154fe13a98871dde5c2fc8a999 Mon Sep 17 00:00:00 2001 From: Andy Grigg Date: Sat, 12 Feb 2022 16:44:39 -0500 Subject: [PATCH 1/7] TST: add test for norm scatter plot parameter (#45809) --- pandas/tests/plotting/frame/test_frame.py | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/pandas/tests/plotting/frame/test_frame.py b/pandas/tests/plotting/frame/test_frame.py index 5cbfb5286bb10..899a63356a9ee 100644 --- a/pandas/tests/plotting/frame/test_frame.py +++ b/pandas/tests/plotting/frame/test_frame.py @@ -807,6 +807,16 @@ def test_plot_scatter_with_s(self): ax = df.plot.scatter(x="a", y="b", s="c") tm.assert_numpy_array_equal(df["c"].values, right=ax.collections[0].get_sizes()) + def test_plot_scatter_with_norm(self): + import matplotlib as mpl + + ax = self.hexbin_df.plot.scatter(x="A", y="B", c="C", norm=mpl.colors.LogNorm()) + assert ax.collections[0].norm._scale + + def test_plot_scatter_without_norm(self): + ax = self.hexbin_df.plot.scatter(x="A", y="B", c="C") + assert ax.collections[0].norm._scale is None + def test_plot_bar(self): df = DataFrame( np.random.randn(6, 4), From 742527e013ef3f4301f0a96c7746fab0907fb6ee Mon Sep 17 00:00:00 2001 From: Andy Grigg Date: Sat, 12 Feb 2022 16:46:17 -0500 Subject: [PATCH 2/7] BUG: don't duplicate norm parameter for scatter plots (#45809) --- pandas/plotting/_matplotlib/core.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pandas/plotting/_matplotlib/core.py b/pandas/plotting/_matplotlib/core.py index e7c49d7bffb14..bcbd9f2c457ec 100644 --- a/pandas/plotting/_matplotlib/core.py +++ b/pandas/plotting/_matplotlib/core.py @@ -1105,7 +1105,7 @@ def _make_plot(self): bounds = np.linspace(0, n_cats, n_cats + 1) norm = colors.BoundaryNorm(bounds, cmap.N) else: - norm = None + norm = self.kwds.pop("norm", None) # plot colorbar if # 1. colormap is assigned, and # 2.`c` is a column containing only numeric values From 082e3f99e24ceacf0a838cb4ded9c1173f8efa30 Mon Sep 17 00:00:00 2001 From: Andy Grigg Date: Tue, 22 Feb 2022 14:10:36 -0500 Subject: [PATCH 3/7] TST: Add github issue numbers (GH45809) --- pandas/tests/plotting/frame/test_frame.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/pandas/tests/plotting/frame/test_frame.py b/pandas/tests/plotting/frame/test_frame.py index f157ce7f44546..e77e5dbbfeaf7 100644 --- a/pandas/tests/plotting/frame/test_frame.py +++ b/pandas/tests/plotting/frame/test_frame.py @@ -788,12 +788,14 @@ def test_plot_scatter_with_s(self): tm.assert_numpy_array_equal(df["c"].values, right=ax.collections[0].get_sizes()) def test_plot_scatter_with_norm(self): + # added while fixing GH 45809 import matplotlib as mpl ax = self.hexbin_df.plot.scatter(x="A", y="B", c="C", norm=mpl.colors.LogNorm()) assert ax.collections[0].norm._scale def test_plot_scatter_without_norm(self): + # added while fixing GH 45809 ax = self.hexbin_df.plot.scatter(x="A", y="B", c="C") assert ax.collections[0].norm._scale is None From 440690691c0dc6d3d91846776455c235cefe6468 Mon Sep 17 00:00:00 2001 From: Andy Grigg Date: Sat, 26 Feb 2022 17:38:49 -0500 Subject: [PATCH 4/7] TST: remove dependence on private attributes (#45809) --- pandas/tests/plotting/frame/test_frame.py | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-) diff --git a/pandas/tests/plotting/frame/test_frame.py b/pandas/tests/plotting/frame/test_frame.py index e77e5dbbfeaf7..21aba0d94051e 100644 --- a/pandas/tests/plotting/frame/test_frame.py +++ b/pandas/tests/plotting/frame/test_frame.py @@ -791,13 +791,21 @@ def test_plot_scatter_with_norm(self): # added while fixing GH 45809 import matplotlib as mpl - ax = self.hexbin_df.plot.scatter(x="A", y="B", c="C", norm=mpl.colors.LogNorm()) - assert ax.collections[0].norm._scale + df = DataFrame(np.random.random((10, 3)) * 100, columns=["a", "b", "c"]) + norm = mpl.colors.LogNorm() + ax = df.plot.scatter(x="a", y="b", c="c", norm=norm) + assert ax.collections[0].norm is norm def test_plot_scatter_without_norm(self): # added while fixing GH 45809 - ax = self.hexbin_df.plot.scatter(x="A", y="B", c="C") - assert ax.collections[0].norm._scale is None + import matplotlib as mpl + + df = DataFrame(np.random.random((10, 3)) * 100, columns=["a", "b", "c"]) + ax = df.plot.scatter(x="a", y="b", c="c") + color_min_max = (df.c.min(), df.c.max()) + default_norm = mpl.colors.Normalize(*color_min_max) + for c in range(100): + assert ax.collections[0].norm(c) == default_norm(c) def test_plot_bar(self): df = DataFrame( From 9ce0c55f0f34e2bcffe20d32c30f7d58e2f4c5ac Mon Sep 17 00:00:00 2001 From: Andy Grigg Date: Sat, 26 Feb 2022 17:49:32 -0500 Subject: [PATCH 5/7] DOC: add entry to visualization bug fixes (#45809) --- doc/source/whatsnew/v1.5.0.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/doc/source/whatsnew/v1.5.0.rst b/doc/source/whatsnew/v1.5.0.rst index 5d656cde08610..de7e8a297f066 100644 --- a/doc/source/whatsnew/v1.5.0.rst +++ b/doc/source/whatsnew/v1.5.0.rst @@ -375,7 +375,7 @@ Plotting - Bug in :meth:`DataFrame.plot.box` that prevented labeling the x-axis (:issue:`45463`) - Bug in :meth:`DataFrame.boxplot` that prevented passing in ``xlabel`` and ``ylabel`` (:issue:`45463`) - Bug in :meth:`DataFrame.boxplot` that prevented specifying ``vert=False`` (:issue:`36918`) -- +- Bug in :meth:`DataFrame.scatter` that prevented specifying ``norm`` (:issue:`45809`) Groupby/resample/rolling ^^^^^^^^^^^^^^^^^^^^^^^^ From 0da09414bef07bd8a833f8425071b4148c2c874c Mon Sep 17 00:00:00 2001 From: Andy Grigg Date: Sun, 27 Feb 2022 11:25:57 -0500 Subject: [PATCH 6/7] TST: reduce number of norm comparisons (#45809) --- pandas/tests/plotting/frame/test_frame.py | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/pandas/tests/plotting/frame/test_frame.py b/pandas/tests/plotting/frame/test_frame.py index 21aba0d94051e..fd5a1daf3dd47 100644 --- a/pandas/tests/plotting/frame/test_frame.py +++ b/pandas/tests/plotting/frame/test_frame.py @@ -804,8 +804,7 @@ def test_plot_scatter_without_norm(self): ax = df.plot.scatter(x="a", y="b", c="c") color_min_max = (df.c.min(), df.c.max()) default_norm = mpl.colors.Normalize(*color_min_max) - for c in range(100): - assert ax.collections[0].norm(c) == default_norm(c) + assert all(df.c.apply(lambda x: ax.collections[0].norm(x) == default_norm(x))) def test_plot_bar(self): df = DataFrame( From 0f32c4ff1882b86329a9beea42c1c9bb7f86f511 Mon Sep 17 00:00:00 2001 From: Andy Grigg Date: Sat, 5 Mar 2022 13:11:00 -0500 Subject: [PATCH 7/7] TST: simplify test (#45809) --- pandas/tests/plotting/frame/test_frame.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/pandas/tests/plotting/frame/test_frame.py b/pandas/tests/plotting/frame/test_frame.py index 267f99567cc2f..62540a15f47bd 100644 --- a/pandas/tests/plotting/frame/test_frame.py +++ b/pandas/tests/plotting/frame/test_frame.py @@ -802,9 +802,11 @@ def test_plot_scatter_without_norm(self): df = DataFrame(np.random.random((10, 3)) * 100, columns=["a", "b", "c"]) ax = df.plot.scatter(x="a", y="b", c="c") + plot_norm = ax.collections[0].norm color_min_max = (df.c.min(), df.c.max()) default_norm = mpl.colors.Normalize(*color_min_max) - assert all(df.c.apply(lambda x: ax.collections[0].norm(x) == default_norm(x))) + for value in df.c: + assert plot_norm(value) == default_norm(value) @pytest.mark.slow def test_plot_bar(self):