pandas 0.7.2 documentation

Plotting with matplotlib

Note

We intend to build more plotting integration with matplotlib as time goes on.

We use the standard convention for referencing the matplotlib API:

In [872]: import matplotlib.pyplot as plt

Basic plotting: plot

The plot method on Series and DataFrame is just a simple wrapper around plt.plot:

In [873]: ts = Series(randn(1000), index=DateRange('1/1/2000', periods=1000))

In [874]: ts = ts.cumsum()

In [875]: ts.plot()
Out[875]: <matplotlib.axes.AxesSubplot at 0x1146fae10>
_images/series_plot_basic.png

If the index consists of dates, it calls gca().autofmt_xdate() to try to format the x-axis nicely as per above. The method takes a number of arguments for controlling the look of the plot:

In [876]: plt.figure(); ts.plot(style='k--', label='Series'); plt.legend()
Out[876]: <matplotlib.legend.Legend at 0x1165b6c90>
_images/series_plot_basic2.png

On DataFrame, plot is a convenience to plot all of the columns with labels:

In [877]: df = DataFrame(randn(1000, 4), index=ts.index,
   .....:                columns=['A', 'B', 'C', 'D'])

In [878]: df = df.cumsum()

In [879]: plt.figure(); df.plot(); plt.legend(loc='best')
Out[879]: <matplotlib.legend.Legend at 0x11978ce90>
_images/frame_plot_basic.png

You may set the legend argument to False to hide the legend, which is shown by default.

In [880]: df.plot(legend=False)
Out[880]: <matplotlib.axes.AxesSubplot at 0x1197dce90>
_images/frame_plot_basic_noleg.png

Some other options are available, like plotting each Series on a different axis:

In [881]: df.plot(subplots=True, figsize=(8, 8)); plt.legend(loc='best')
Out[881]: <matplotlib.legend.Legend at 0x1197dd1d0>
_images/frame_plot_subplots.png

You may pass logy to get a log-scale Y axis.

In [882]: plt.figure();
In [882]: ts = Series(randn(1000), index=DateRange('1/1/2000', periods=1000))

In [883]: ts = np.exp(ts.cumsum())

In [884]: ts.plot(logy=True)
Out[884]: <matplotlib.axes.AxesSubplot at 0x11ba81b10>
_images/series_plot_logy.png

Targeting different subplots

You can pass an ax argument to Series.plot to plot on a particular axis:

In [885]: fig, axes = plt.subplots(nrows=2, ncols=2, figsize=(8, 5))

In [886]: df['A'].plot(ax=axes[0,0]); axes[0,0].set_title('A')
Out[886]: <matplotlib.text.Text at 0x11bb29dd0>

In [887]: df['B'].plot(ax=axes[0,1]); axes[0,1].set_title('B')
Out[887]: <matplotlib.text.Text at 0x11bd6e290>

In [888]: df['C'].plot(ax=axes[1,0]); axes[1,0].set_title('C')
Out[888]: <matplotlib.text.Text at 0x11bb1a350>

In [889]: df['D'].plot(ax=axes[1,1]); axes[1,1].set_title('D')
Out[889]: <matplotlib.text.Text at 0x11bd8a690>
_images/series_plot_multi.png

Other plotting features

Plotting non-time series data

For labeled, non-time series data, you may wish to produce a bar plot:

In [890]: plt.figure();
In [890]: df.ix[5].plot(kind='bar'); plt.axhline(0, color='k')
Out[890]: <matplotlib.lines.Line2D at 0x11966d950>
_images/bar_plot_ex.png

Histogramming

In [891]: plt.figure();
In [891]: df['A'].diff().hist()
Out[891]: <matplotlib.axes.AxesSubplot at 0x11bc4bb50>
_images/hist_plot_ex.png

For a DataFrame, hist plots the histograms of the columns on multiple subplots:

In [892]: plt.figure()
Out[892]: <matplotlib.figure.Figure at 0x11bc7f790>

In [893]: df.diff().hist(color='k', alpha=0.5, bins=50)
Out[893]: 
array([[Axes(0.125,0.536364;0.352273x0.363636),
        Axes(0.547727,0.536364;0.352273x0.363636)],
       [Axes(0.125,0.1;0.352273x0.363636),
        Axes(0.547727,0.1;0.352273x0.363636)]], dtype=object)
_images/frame_hist_ex.png

Box-Plotting

DataFrame has a boxplot method which allows you to visualize the distribution of values within each column.

For instance, here is a boxplot representing five trials of 10 observations of a uniform random variable on [0,1).

In [894]: df = DataFrame(np.random.rand(10,5))

In [895]: plt.figure();
In [895]: df.boxplot()
Out[895]: <matplotlib.axes.AxesSubplot at 0x11965ef10>
_images/box_plot_ex.png

You can create a stratified boxplot using the by keyword argument to create groupings. For instance,

In [896]: df = DataFrame(np.random.rand(10,2), columns=['Col1', 'Col2'] )

In [897]: df['X'] = Series(['A','A','A','A','A','B','B','B','B','B'])

In [898]: plt.figure();
In [898]: df.boxplot(by='X')
Out[898]: array([Axes(0.1,0.15;0.363636x0.75), Axes(0.536364,0.15;0.363636x0.75)], dtype=object)
_images/box_plot_ex2.png

You can also pass a subset of columns to plot, as well as group by multiple columns:

In [899]: df = DataFrame(np.random.rand(10,3), columns=['Col1', 'Col2', 'Col3'])

In [900]: df['X'] = Series(['A','A','A','A','A','B','B','B','B','B'])

In [901]: df['Y'] = Series(['A','B','A','B','A','B','A','B','A','B'])

In [902]: plt.figure();
In [902]: df.boxplot(column=['Col1','Col2'], by=['X','Y'])
Out[902]: array([Axes(0.1,0.15;0.363636x0.75), Axes(0.536364,0.15;0.363636x0.75)], dtype=object)
_images/box_plot_ex3.png