Skip to content

Commit 9ee2139

Browse files
jorisvandenbosschejreback
authored andcommitted
VIS: only apply shared axes handling on actual SubplotAxes
To fix bugs when dealing with plain Axes objects, #11556, #11520
1 parent 35b159d commit 9ee2139

File tree

3 files changed

+37
-4
lines changed

3 files changed

+37
-4
lines changed

doc/source/whatsnew/v0.17.1.txt

+2
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,8 @@ Bug Fixes
140140

141141

142142

143+
- Fix plotting issues when having plain ``Axes`` instances instead of
144+
``SubplotAxes`` (:issue:`11520`, :issue:`11556`).
143145

144146

145147

pandas/tests/test_graphics.py

+29
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

+6-4
Original file line numberDiff line numberDiff line change
@@ -1137,7 +1137,7 @@ def _post_plot_logic(self, ax, data):
11371137
def _adorn_subplots(self):
11381138
"""Common post process unrelated to data"""
11391139
if len(self.axes) > 0:
1140-
all_axes = self._get_axes()
1140+
all_axes = self._get_subplots()
11411141
nrows, ncols = self._get_axes_layout()
11421142
_handle_shared_axes(axarr=all_axes, nplots=len(all_axes),
11431143
naxes=nrows * ncols, nrows=nrows,
@@ -1469,11 +1469,13 @@ def _get_errorbars(self, label=None, index=None, xerr=True, yerr=True):
14691469
errors[kw] = err
14701470
return errors
14711471

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

14751477
def _get_axes_layout(self):
1476-
axes = self._get_axes()
1478+
axes = self._get_subplots()
14771479
x_set = set()
14781480
y_set = set()
14791481
for ax in axes:

0 commit comments

Comments
 (0)