Skip to content

Commit 3f7a385

Browse files
javadnoorbjreback
authored andcommitted
misplaced scatter/hexbin subplot colorbars (#21728)
1 parent b87b5fd commit 3f7a385

File tree

3 files changed

+22
-3
lines changed

3 files changed

+22
-3
lines changed

doc/source/whatsnew/v0.24.0.txt

+1-2
Original file line numberDiff line numberDiff line change
@@ -336,8 +336,7 @@ I/O
336336
Plotting
337337
^^^^^^^^
338338

339-
- 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`)
340-
-
339+
- 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`)
341340
-
342341

343342
Groupby/Resample/Rolling

pandas/plotting/_core.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -847,7 +847,7 @@ def _plot_colorbar(self, ax, **kwds):
847847
# https://github.com/ipython/ipython/issues/11215
848848

849849
img = ax.collections[0]
850-
cbar = self.fig.colorbar(img, **kwds)
850+
cbar = self.fig.colorbar(img, ax=ax, **kwds)
851851
points = ax.get_position().get_points()
852852
cbar_points = cbar.ax.get_position().get_points()
853853
cbar.ax.set_position([cbar_points[0, 0],

pandas/tests/plotting/test_frame.py

+20
Original file line numberDiff line numberDiff line change
@@ -1132,6 +1132,26 @@ def test_if_hexbin_xaxis_label_is_visible(self):
11321132
ax.xaxis.get_majorticklabels()])
11331133
assert ax.xaxis.get_label().get_visible()
11341134

1135+
@pytest.mark.slow
1136+
def test_if_scatterplot_colorbars_are_next_to_parent_axes(self):
1137+
import matplotlib.pyplot as plt
1138+
random_array = np.random.random((1000, 3))
1139+
df = pd.DataFrame(random_array,
1140+
columns=['A label', 'B label', 'C label'])
1141+
1142+
fig, axes = plt.subplots(1, 2)
1143+
df.plot.scatter('A label', 'B label', c='C label', ax=axes[0])
1144+
df.plot.scatter('A label', 'B label', c='C label', ax=axes[1])
1145+
plt.tight_layout()
1146+
1147+
points = np.array([ax.get_position().get_points()
1148+
for ax in fig.axes])
1149+
axes_x_coords = points[:, :, 0]
1150+
parent_distance = axes_x_coords[1, :] - axes_x_coords[0, :]
1151+
colorbar_distance = axes_x_coords[3, :] - axes_x_coords[2, :]
1152+
assert np.isclose(parent_distance,
1153+
colorbar_distance, atol=1e-7).all()
1154+
11351155
@pytest.mark.slow
11361156
def test_plot_scatter_with_categorical_data(self):
11371157
# GH 16199

0 commit comments

Comments
 (0)