Plotting fails when using subplot grid + manual axis #11556

Closed
jakevdp opened this Issue Nov 9, 2015 · 6 comments

Comments

Projects
None yet
3 participants
Contributor

jakevdp commented Nov 9, 2015

The following code raises an error (tested on pandas 0.17.0, matplotlib 1.4.3, and Python 3.4.2)

>>> fig, ax = plt.subplots()
>>> fig.add_axes([0.2, 0.2, 0.2, 0.2])
>>> pd.Series(np.random.rand(100)).plot(ax=ax)
---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
<ipython-input-13-80531efe48c4> in <module>()
      8 fig, ax = plt.subplots()
      9 inset = fig.add_axes([0.2, 0.2, 0.2, 0.2])
---> 10 data.plot(ax=ax)

/Users/jakevdp/anaconda/envs/python3.4/lib/python3.4/site-packages/pandas/tools/plotting.py in __call__(self, kind, ax, figsize, use_index, title, grid, legend, style, logx, logy, loglog, xticks, yticks, xlim, ylim, rot, fontsize, colormap, table, yerr, xerr, label, secondary_y, **kwds)
   3491                            colormap=colormap, table=table, yerr=yerr,
   3492                            xerr=xerr, label=label, secondary_y=secondary_y,
-> 3493                            **kwds)
   3494     __call__.__doc__ = plot_series.__doc__
   3495 

/Users/jakevdp/anaconda/envs/python3.4/lib/python3.4/site-packages/pandas/tools/plotting.py in plot_series(data, kind, ax, figsize, use_index, title, grid, legend, style, logx, logy, loglog, xticks, yticks, xlim, ylim, rot, fontsize, colormap, table, yerr, xerr, label, secondary_y, **kwds)
   2581                  yerr=yerr, xerr=xerr,
   2582                  label=label, secondary_y=secondary_y,
-> 2583                  **kwds)
   2584 
   2585 

/Users/jakevdp/anaconda/envs/python3.4/lib/python3.4/site-packages/pandas/tools/plotting.py in _plot(data, x, y, subplots, ax, kind, **kwds)
   2378         plot_obj = klass(data, subplots=subplots, ax=ax, kind=kind, **kwds)
   2379 
-> 2380     plot_obj.generate()
   2381     plot_obj.draw()
   2382     return plot_obj.result

/Users/jakevdp/anaconda/envs/python3.4/lib/python3.4/site-packages/pandas/tools/plotting.py in generate(self)
    990             self._post_plot_logic_common(ax, self.data)
    991             self._post_plot_logic(ax, self.data)
--> 992         self._adorn_subplots()
    993 
    994     def _args_adjust(self):

/Users/jakevdp/anaconda/envs/python3.4/lib/python3.4/site-packages/pandas/tools/plotting.py in _adorn_subplots(self)
   1141                                 naxes=nrows * ncols, nrows=nrows,
   1142                                 ncols=ncols, sharex=self.sharex,
-> 1143                                 sharey=self.sharey)
   1144 
   1145         for ax in self.axes:

/Users/jakevdp/anaconda/envs/python3.4/lib/python3.4/site-packages/pandas/tools/plotting.py in _handle_shared_axes(axarr, nplots, naxes, nrows, ncols, sharex, sharey)
   3378                 layout = np.zeros((nrows+1,ncols+1), dtype=np.bool)
   3379                 for ax in axarr:
-> 3380                     layout[ax.rowNum, ax.colNum] = ax.get_visible()
   3381 
   3382                 for ax in axarr:

AttributeError: 'Axes' object has no attribute 'rowNum'

I think this is a duplicate of #11520 (or at least related to), i.e. we assume in the code that the ax is an AxesSubplot instead of a plain Axes

Contributor

jakevdp commented Nov 9, 2015

In this case the ax actually is an AxesSubplot. It's the very presence of another non-subplot axes that causes the error. That is, if you comment out the line:

fig, ax = plt.subplots()
# fig.add_axes([0.2, 0.2, 0.2, 0.2])
pd.Series(np.random.rand(100)).plot(ax=ax)

then there is no error.

Ah, yes, I missed the fact that it was not the add_axes created ax you passed to plot.

But in any case, the underlying issue is still the same, that in adorn_subplots all axes of the figure are inspected and assumed to be AxesSubplots. But because of the this, eg skipping this step if the supplied ax is not a SubplotAxes, will not be sufficient to fix this.

@sinhrks @TomAugspurger @janschulz (you did quite a lot of work on this in #9740)

Maybe the easiest fix for this problem and of #11520 is that we only apply all the specific handling in _handle_shared_axes to actual SubplotAxes, and disregard of plain Axes? (but not really sure about its implications in all possible corner cases)

What I mean is something like this: f906c08 This fixes at least the problem @jakevdp experiences

jorisvandenbossche added this to the 0.17.1 milestone Nov 9, 2015

@jorisvandenbossche jorisvandenbossche added a commit that referenced this issue Nov 15, 2015

@jorisvandenbossche @jreback jorisvandenbossche + jreback VIS: only apply shared axes handling on actual SubplotAxes
To fix bugs when dealing with plain Axes objects, #11556, #11520
9ee2139
Contributor

jreback commented Nov 15, 2015

closed by #11561

jreback closed this Nov 15, 2015

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment