Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ENH: Allow subplots axes to be passed into DataFrame.plot() #6306

Closed
moorepants opened this issue Feb 8, 2014 · 4 comments
Closed

ENH: Allow subplots axes to be passed into DataFrame.plot() #6306

moorepants opened this issue Feb 8, 2014 · 4 comments
Labels

Comments

@moorepants
Copy link

This works nicely:

Python 2.7.5+ (default, Sep 19 2013, 13:48:49) 
Type "copyright", "credits" or "license" for more information.

IPython 1.1.0 -- An enhanced Interactive Python.
?         -> Introduction and overview of IPython's features.
%quickref -> Quick reference.
help      -> Python's own help system.
object?   -> Details about 'object', use 'object??' for extra details.

In [1]: import pandas, numpy

In [2]: import matplotlib.pyplot as plt

In [3]: df1 = pandas.DataFrame(numpy.random.random((5, 4)))

In [4]: df2 = pandas.DataFrame(numpy.random.random((5, 4)))

In [5]: ax = df1.plot()

In [6]: df2.plot(ax=ax)
Out[6]: <matplotlib.axes.AxesSubplot at 0x3f4f910>

In [7]: plt.show()

But this doesn't:

In [8]: ax = df1.plot(subplots=True)

In [9]: df2.plot(subplots=True, ax=ax)
---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
<ipython-input-9-70822353a5d1> in <module>()
----> 1 df2.plot(subplots=True, ax=ax)

/usr/local/lib/python2.7/dist-packages/pandas/tools/plotting.pyc in plot_frame(frame, x, y, subplots, sharex, sharey, use_index, figsize, grid, legend, rot, ax, style, title, xlim, ylim, logx, logy, xticks, yticks, kind, sort_columns, fontsize, secondary_y, **kwds)
   1634                      logy=logy, sort_columns=sort_columns,
   1635                      secondary_y=secondary_y, **kwds)
-> 1636     plot_obj.generate()
   1637     plot_obj.draw()
   1638     if subplots:

/usr/local/lib/python2.7/dist-packages/pandas/tools/plotting.pyc in generate(self)
    853         self._args_adjust()
    854         self._compute_plot_data()
--> 855         self._setup_subplots()
    856         self._make_plot()
    857         self._post_plot_logic()

/usr/local/lib/python2.7/dist-packages/pandas/tools/plotting.pyc in _setup_subplots(self)
    909                 ax = self._maybe_right_yaxis(ax)
    910             else:
--> 911                 fig = self.ax.get_figure()
    912                 if self.figsize is not None:
    913                     fig.set_size_inches(self.figsize)

AttributeError: 'numpy.ndarray' object has no attribute 'get_figure'

It would be nice if you could use the subplots flag, because it makes it easier to compare two data frames visually.

@TomAugspurger
Copy link
Contributor

This seems a bit tricky API wise. We'd need to figure out what to do if df2 and df1 are not indexed the same.

What would you think about having more control over the layout if you had a MultiIndex for the columns?

df3 = pd.concat([df1, df2], axis=1, keys=['a', 'b'])
In [1]: df3
Out[68]: 
          a                                       b                      \
          0         1         2         3         0         1         2   
0  0.403222  0.179409  0.819205  0.839402  0.421360  0.334930  0.251081   
1  0.689900  0.969358  0.247526  0.670381  0.509042  0.237918  0.794933   
2  0.998004  0.965907  0.354623  0.109929  0.160774  0.130975  0.591787   
3  0.761342  0.420306  0.571551  0.109936  0.581581  0.925967  0.586253   
4  0.750932  0.624856  0.975460  0.860216  0.414069  0.883192  0.668196   


          3  
0  0.952108  
1  0.420736  
2  0.378974  
3  0.037831  
4  0.706987  

[5 rows x 8 columns]

And then you could to subplots by a level of the MultiIndex? That removes any ambiguity of differently index df1 and df2.


as a workaround for now:

axes = df1.plot(subplots=True)

for i, ax in enumerate(axes):
    df2[i].plot(ax=ax)

plt.draw()

@moorepants
Copy link
Author

This works:

axes = df1.plot(subplots=True)

for i, ax in enumerate(axes):
    df2.iloc[:, i].plot(ax=ax)

plt.draw()

The multiindex idea sounds good too.

@jreback
Copy link
Contributor

jreback commented Mar 22, 2014

#6667 will make this possible I think

@jreback
Copy link
Contributor

jreback commented May 16, 2014

dupe of #6667

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

3 participants