In [1]:
#coding: utf-8
%matplotlib inline
import numpy as np
import pandas as pd
from pandas import Series, DataFrame
import pylab as pl
import matplotlib.pyplot as plt
plt.style.use('ggplot')

Basic Plotting

In [2]:
ts = pd.Series(np.random.randn(1000), index=pd.date_range('1/1/2000', periods=1000))
In [3]:
ts = ts.cumsum()
In [4]:
ts.plot()
Out[4]:
<matplotlib.axes._subplots.AxesSubplot at 0x7648080>
In [5]:
plt.plot(ts)
Out[5]:
[<matplotlib.lines.Line2D at 0x7be98d0>]
In [6]:
df = pd.DataFrame(np.random.randn(1000, 4), index=pd.date_range('1/1/2000', periods=1000), columns=list('ABCD'))
In [7]:
df = df.cumsum()
In [8]:
df.plot()
Out[8]:
<matplotlib.axes._subplots.AxesSubplot at 0x7bd2748>
In [9]:
df.head()
Out[9]:
A B C D
2000-01-01 0.023085 0.987579 -1.661717 1.000657
2000-01-02 0.749531 0.935092 -2.607361 3.196274
2000-01-03 -0.369244 0.805470 -4.017652 2.056036
2000-01-04 -3.466182 0.629565 -3.710043 3.023316
2000-01-05 -4.307879 -1.363878 -4.818511 3.106890
In [10]:
# df['E'] = pd.Series(list(range(len(df))))
In [11]:
df['E'] = pd.Series(list(range(len(df))), index=df.index)
In [12]:
df.head()
Out[12]:
A B C D E
2000-01-01 0.023085 0.987579 -1.661717 1.000657 0
2000-01-02 0.749531 0.935092 -2.607361 3.196274 1
2000-01-03 -0.369244 0.805470 -4.017652 2.056036 2
2000-01-04 -3.466182 0.629565 -3.710043 3.023316 3
2000-01-05 -4.307879 -1.363878 -4.818511 3.106890 4
In [13]:
df.plot(x='E', y='A')
Out[13]:
<matplotlib.axes._subplots.AxesSubplot at 0x8072f28>

Pie plot

In [14]:
rs = pd.Series(3 * np.random.rand(4), index=['a', 'b', 'c', 'd'], name='series')
In [15]:
rs.plot.pie(figsize=(6, 6))
Out[15]:
<matplotlib.axes._subplots.AxesSubplot at 0x8d1f908>
In [16]:
df = pd.DataFrame(3 * np.random.rand(4, 2), index=['a', 'b', 'c', 'd'], columns=['x', 'y'])
In [17]:
df.plot.pie(subplots=True, figsize=(8, 4))
Out[17]:
array([<matplotlib.axes._subplots.AxesSubplot object at 0x0000000008FA5438>,
       <matplotlib.axes._subplots.AxesSubplot object at 0x000000000923A0B8>], dtype=object)

Scatter Plots

In [18]:
df = pd.DataFrame(np.random.rand(50, 4), columns=['a', 'b', 'c', 'd'])
In [19]:
df.plot.scatter(x='a', y='b')
Out[19]:
<matplotlib.axes._subplots.AxesSubplot at 0x94f9860>
In [20]:
ax = df.plot.scatter(x='a', y='b', color='DarkBlue', label='Group 1')
In [21]:
df.plot.scatter(x='c', y='d', color='DarkGreen', label='Group 2', ax=
               df.plot.scatter(x='a', y='b', color='DarkBlue', label='Group 1'))
Out[21]:
<matplotlib.axes._subplots.AxesSubplot at 0x977c518>
In [22]:
df.plot.scatter(x='a', y='b', c='c', s=50)
Out[22]:
<matplotlib.axes._subplots.AxesSubplot at 0x9a64c88>
In [23]:
df.plot.scatter(x='a', y='b', s=df['c']*200)
Out[23]:
<matplotlib.axes._subplots.AxesSubplot at 0x9dfeb70>

Bar plots

In [24]:
df = pd.DataFrame(np.random.rand(10, 4), columns=['a', 'b', 'c', 'd'])
In [25]:
df.head()
Out[25]:
a b c d
0 0.735818 0.271893 0.334232 0.990616
1 0.097871 0.666296 0.752410 0.885170
2 0.756239 0.775269 0.528630 0.491660
3 0.431590 0.401146 0.863193 0.726383
4 0.615300 0.067489 0.892783 0.490840
In [26]:
df.ix[3].plot(kind='bar')
Out[26]:
<matplotlib.axes._subplots.AxesSubplot at 0x9e12fd0>
In [27]:
df.ix[3].plot.bar()
Out[27]:
<matplotlib.axes._subplots.AxesSubplot at 0xa1bbc88>
In [28]:
df.plot.bar()
Out[28]:
<matplotlib.axes._subplots.AxesSubplot at 0xa132ef0>
In [29]:
df.plot.bar(stacked=True)
Out[29]:
<matplotlib.axes._subplots.AxesSubplot at 0xa8382e8>
In [30]:
df.plot.barh(stacked=True)
Out[30]:
<matplotlib.axes._subplots.AxesSubplot at 0xaaa0ba8>

Histograms

In [31]:
df = pd.DataFrame({'a': np.random.randn(1000) + 1, 'b': np.random.randn(1000),
                    'c': np.random.randn(1000) - 1}, columns=['a', 'b', 'c'])
In [32]:
df.head()
Out[32]:
a b c
0 0.375014 1.480655 -0.826677
1 0.178139 -0.277242 -0.821195
2 0.411861 0.170729 -1.392080
3 2.000135 0.611680 -0.136498
4 0.201207 1.036948 -0.785128
In [33]:
df.plot.hist(alpha=0.5)
Out[33]:
<matplotlib.axes._subplots.AxesSubplot at 0xaf318d0>
In [34]:
df.plot.hist(alpha=0.5, stacked=True)
Out[34]:
<matplotlib.axes._subplots.AxesSubplot at 0xb339208>
In [35]:
df.plot.hist(alpha=0.5, stacked=True, bins=20)
Out[35]:
<matplotlib.axes._subplots.AxesSubplot at 0xad6d780>
In [36]:
df['a'].plot.hist(orientation='horizontal', cumulative=True)
Out[36]:
<matplotlib.axes._subplots.AxesSubplot at 0xb8b3b70>
In [37]:
df['a'].plot.hist(orientation='horizontal', cumulative=False)
Out[37]:
<matplotlib.axes._subplots.AxesSubplot at 0xbccce48>
In [38]:
df['a'].diff().hist()
Out[38]:
<matplotlib.axes._subplots.AxesSubplot at 0xbf19128>
In [39]:
df.diff().hist(color='k', alpha=0.5, bins=50)
Out[39]:
array([[<matplotlib.axes._subplots.AxesSubplot object at 0x000000000C281CF8>,
        <matplotlib.axes._subplots.AxesSubplot object at 0x000000000C3E1D68>],
       [<matplotlib.axes._subplots.AxesSubplot object at 0x000000000C4E8630>,
        <matplotlib.axes._subplots.AxesSubplot object at 0x000000000C5C8D68>]], dtype=object)
In [40]:
data = pd.Series(np.random.randn(1000))
In [41]:
data.hist(by=np.random.randint(0, 4, 1000), figsize=(6, 4))
Out[41]:
array([[<matplotlib.axes._subplots.AxesSubplot object at 0x000000000CCEC3C8>,
        <matplotlib.axes._subplots.AxesSubplot object at 0x000000000CE564E0>],
       [<matplotlib.axes._subplots.AxesSubplot object at 0x000000000CF6C978>,
        <matplotlib.axes._subplots.AxesSubplot object at 0x000000000D059828>]], dtype=object)