Skip to content

Commit a145a59

Browse files
VIS: only apply shared axes handling on actual SubplotAxes
To fix bugs when dealing with plain Axes objects (GH11556, GH11520)
1 parent 946af10 commit a145a59

File tree

3 files changed

+37
-5
lines changed

3 files changed

+37
-5
lines changed

doc/source/whatsnew/v0.17.1.txt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,8 @@ Bug Fixes
102102

103103

104104
- Fix regression in setting of ``xticks`` in ``plot`` (:issue:`11529`).
105-
105+
- Fix plotting issues when having plain ``Axes`` instances instead of
106+
``SubplotAxes`` (:issue:`11520`, :issue:`11556`).
106107

107108

108109

pandas/tests/test_graphics.py

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3678,6 +3678,35 @@ def test_invalid_colormap(self):
36783678
with tm.assertRaises(ValueError):
36793679
df.plot(colormap='invalid_colormap')
36803680

3681+
def test_plain_axes(self):
3682+
3683+
# supplied ax itself is a SubplotAxes, but figure contains also
3684+
# a plain Axes object (GH11556)
3685+
fig, ax = self.plt.subplots()
3686+
fig.add_axes([0.2, 0.2, 0.2, 0.2])
3687+
Series(rand(10)).plot(ax=ax)
3688+
3689+
# suppliad ax itself is a plain Axes, but because the cmap keyword
3690+
# a new ax is created for the colorbar -> also multiples axes (GH11520)
3691+
df = DataFrame({'a': randn(8), 'b': randn(8)})
3692+
fig = self.plt.figure()
3693+
ax = fig.add_axes((0,0,1,1))
3694+
df.plot(kind='scatter', ax=ax, x='a', y='b', c='a', cmap='hsv')
3695+
3696+
# other examples
3697+
fig, ax = self.plt.subplots()
3698+
from mpl_toolkits.axes_grid1 import make_axes_locatable
3699+
divider = make_axes_locatable(ax)
3700+
cax = divider.append_axes("right", size="5%", pad=0.05)
3701+
Series(rand(10)).plot(ax=ax)
3702+
Series(rand(10)).plot(ax=cax)
3703+
3704+
fig, ax = self.plt.subplots()
3705+
from mpl_toolkits.axes_grid.inset_locator import inset_axes
3706+
iax = inset_axes(ax, width="30%", height=1., loc=3)
3707+
Series(rand(10)).plot(ax=ax)
3708+
Series(rand(10)).plot(ax=iax)
3709+
36813710

36823711
@tm.mplskip
36833712
class TestDataFrameGroupByPlots(TestPlotBase):

pandas/tools/plotting.py

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1135,7 +1135,7 @@ def _post_plot_logic(self, ax, data):
11351135
def _adorn_subplots(self):
11361136
"""Common post process unrelated to data"""
11371137
if len(self.axes) > 0:
1138-
all_axes = self._get_axes()
1138+
all_axes = self._get_subplots()
11391139
nrows, ncols = self._get_axes_layout()
11401140
_handle_shared_axes(axarr=all_axes, nplots=len(all_axes),
11411141
naxes=nrows * ncols, nrows=nrows,
@@ -1467,11 +1467,13 @@ def _get_errorbars(self, label=None, index=None, xerr=True, yerr=True):
14671467
errors[kw] = err
14681468
return errors
14691469

1470-
def _get_axes(self):
1471-
return self.axes[0].get_figure().get_axes()
1470+
def _get_subplots(self):
1471+
from matplotlib.axes import Subplot
1472+
return [ax for ax in self.axes[0].get_figure().get_axes()
1473+
if isinstance(ax, Subplot)]
14721474

14731475
def _get_axes_layout(self):
1474-
axes = self._get_axes()
1476+
axes = self._get_subplots()
14751477
x_set = set()
14761478
y_set = set()
14771479
for ax in axes:

0 commit comments

Comments
 (0)