From 580aad45217af9a53bf3e38f886a733d37efb9b2 Mon Sep 17 00:00:00 2001 From: Javad Date: Wed, 4 Jul 2018 12:13:40 -0400 Subject: [PATCH 1/4] misplaced scatter/hexbin subplot colorbars --- doc/source/whatsnew/v0.24.0.txt | 2 +- pandas/plotting/_core.py | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/doc/source/whatsnew/v0.24.0.txt b/doc/source/whatsnew/v0.24.0.txt index 1b2033999d67d..b45654ef6d895 100644 --- a/doc/source/whatsnew/v0.24.0.txt +++ b/doc/source/whatsnew/v0.24.0.txt @@ -337,7 +337,7 @@ Plotting ^^^^^^^^ - Bug in :func:'DataFrame.plot.scatter' and :func:'DataFrame.plot.hexbin' caused x-axis label and ticklabels to disappear when colorbar was on in IPython inline backend (:issue:`10611` and :issue:`10678`) -- +- Bug in :func:'DataFrame.plot.scatter' and :func:'DataFrame.plot.hexbin' resulted in misplaced colorbars when subplot was used (related to :issue:`20455`) - Groupby/Resample/Rolling diff --git a/pandas/plotting/_core.py b/pandas/plotting/_core.py index 8c2ee90014302..0fb1f2b23faac 100644 --- a/pandas/plotting/_core.py +++ b/pandas/plotting/_core.py @@ -847,7 +847,7 @@ def _plot_colorbar(self, ax, **kwds): # https://github.com/ipython/ipython/issues/11215 img = ax.collections[0] - cbar = self.fig.colorbar(img, **kwds) + cbar = self.fig.colorbar(img, ax=ax, **kwds) points = ax.get_position().get_points() cbar_points = cbar.ax.get_position().get_points() cbar.ax.set_position([cbar_points[0, 0], From 79f90f4b2d2ee44d6879469627eae8603347f0d4 Mon Sep 17 00:00:00 2001 From: Javad Date: Wed, 4 Jul 2018 23:31:23 -0400 Subject: [PATCH 2/4] test to confirm proper location of colorbar --- doc/source/whatsnew/v0.24.0.txt | 3 +-- pandas/tests/plotting/test_frame.py | 18 ++++++++++++++++++ 2 files changed, 19 insertions(+), 2 deletions(-) diff --git a/doc/source/whatsnew/v0.24.0.txt b/doc/source/whatsnew/v0.24.0.txt index b45654ef6d895..4704be34171ed 100644 --- a/doc/source/whatsnew/v0.24.0.txt +++ b/doc/source/whatsnew/v0.24.0.txt @@ -336,8 +336,7 @@ I/O Plotting ^^^^^^^^ -- Bug in :func:'DataFrame.plot.scatter' and :func:'DataFrame.plot.hexbin' caused x-axis label and ticklabels to disappear when colorbar was on in IPython inline backend (:issue:`10611` and :issue:`10678`) -- Bug in :func:'DataFrame.plot.scatter' and :func:'DataFrame.plot.hexbin' resulted in misplaced colorbars when subplot was used (related to :issue:`20455`) +- Bug in :func:'DataFrame.plot.scatter' and :func:'DataFrame.plot.hexbin' caused x-axis label and ticklabels to disappear when colorbar was on in IPython inline backend (:issue:`10611`, :issue:`10678`, and :issue:`20455`) - Groupby/Resample/Rolling diff --git a/pandas/tests/plotting/test_frame.py b/pandas/tests/plotting/test_frame.py index 8ef0cf7154b88..a9a1054cc381f 100644 --- a/pandas/tests/plotting/test_frame.py +++ b/pandas/tests/plotting/test_frame.py @@ -1132,6 +1132,24 @@ def test_if_hexbin_xaxis_label_is_visible(self): ax.xaxis.get_majorticklabels()]) assert ax.xaxis.get_label().get_visible() + @pytest.mark.slow + def test_if_scatterplot_colorbars_are_next_to_parent_axes(): + import matplotlib.pyplot as plt + random_array = np.random.random((1000, 3)) + df = pd.DataFrame(random_array, + columns=['A label', 'B label', 'C label']) + + fig, axes = plt.subplots(1, 2) + df.plot.scatter('A label', 'B label', c='C label', ax=axes[0]) + df.plot.scatter('A label', 'B label', c='C label', ax=axes[1]) + plt.tight_layout() + + points = np.array([ax.get_position().get_points() for ax in fig.axes]) + axes_x_coords = points[:, :, 0] + parent_distance = axes_x_coords[1, :] - axes_x_coords[0, :] + colorbar_distance = axes_x_coords[3, :] - axes_x_coords[2, :] + assert np.isclose(parent_distance, colorbar_distance).all() + @pytest.mark.slow def test_plot_scatter_with_categorical_data(self): # GH 16199 From f5e87cab88e256a32e998dd882928c2441db39b4 Mon Sep 17 00:00:00 2001 From: Javad Date: Wed, 4 Jul 2018 23:38:48 -0400 Subject: [PATCH 3/4] test to confirm proper location of colorbar --- pandas/tests/plotting/test_frame.py | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/pandas/tests/plotting/test_frame.py b/pandas/tests/plotting/test_frame.py index a9a1054cc381f..e64fe278c1170 100644 --- a/pandas/tests/plotting/test_frame.py +++ b/pandas/tests/plotting/test_frame.py @@ -1133,7 +1133,7 @@ def test_if_hexbin_xaxis_label_is_visible(self): assert ax.xaxis.get_label().get_visible() @pytest.mark.slow - def test_if_scatterplot_colorbars_are_next_to_parent_axes(): + def test_if_scatterplot_colorbars_are_next_to_parent_axes(self): import matplotlib.pyplot as plt random_array = np.random.random((1000, 3)) df = pd.DataFrame(random_array, @@ -1144,11 +1144,13 @@ def test_if_scatterplot_colorbars_are_next_to_parent_axes(): df.plot.scatter('A label', 'B label', c='C label', ax=axes[1]) plt.tight_layout() - points = np.array([ax.get_position().get_points() for ax in fig.axes]) + points = np.array([ax.get_position().get_points() + for ax in fig.axes]) axes_x_coords = points[:, :, 0] parent_distance = axes_x_coords[1, :] - axes_x_coords[0, :] colorbar_distance = axes_x_coords[3, :] - axes_x_coords[2, :] - assert np.isclose(parent_distance, colorbar_distance).all() + np.testing.assert_almost_equal( + parent_distance, colorbar_distance, decimal=7) @pytest.mark.slow def test_plot_scatter_with_categorical_data(self): From ad2b52abf17cffad853c30e740e5370a0fc793c2 Mon Sep 17 00:00:00 2001 From: Javad Date: Thu, 5 Jul 2018 09:49:26 -0400 Subject: [PATCH 4/4] lint err: avoid np.testing.assert_almost_equal --- pandas/tests/plotting/test_frame.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/pandas/tests/plotting/test_frame.py b/pandas/tests/plotting/test_frame.py index e64fe278c1170..f1ea847e76091 100644 --- a/pandas/tests/plotting/test_frame.py +++ b/pandas/tests/plotting/test_frame.py @@ -1149,8 +1149,8 @@ def test_if_scatterplot_colorbars_are_next_to_parent_axes(self): axes_x_coords = points[:, :, 0] parent_distance = axes_x_coords[1, :] - axes_x_coords[0, :] colorbar_distance = axes_x_coords[3, :] - axes_x_coords[2, :] - np.testing.assert_almost_equal( - parent_distance, colorbar_distance, decimal=7) + assert np.isclose(parent_distance, + colorbar_distance, atol=1e-7).all() @pytest.mark.slow def test_plot_scatter_with_categorical_data(self):