diff --git a/doc/source/whatsnew/v0.24.0.txt b/doc/source/whatsnew/v0.24.0.txt index 1b2033999d67d..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' 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/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], diff --git a/pandas/tests/plotting/test_frame.py b/pandas/tests/plotting/test_frame.py index 8ef0cf7154b88..f1ea847e76091 100644 --- a/pandas/tests/plotting/test_frame.py +++ b/pandas/tests/plotting/test_frame.py @@ -1132,6 +1132,26 @@ 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(self): + 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, atol=1e-7).all() + @pytest.mark.slow def test_plot_scatter_with_categorical_data(self): # GH 16199