Plotting


In [46]:
import matplotlib.pyplot as plt
import pandas as pd
import numpy as np
from IPython.display import display_html
%matplotlib inline

ts = pd.Series(np.random.randn(1000), index=pd.date_range('1/1/2000', periods=1000))
ts = ts.cumsum()
ts.plot()


Out[46]:
<matplotlib.axes.AxesSubplot at 0x7f5564a7cad0>

In [27]:
df = pd.DataFrame(ts, columns=['A'])
df['B'] = df['A'] ** 2
df['C'] = np.exp(df['B'])
df.cumsum().plot(x='A', y='B', figsize=(6, 4))


Out[27]:
<matplotlib.axes.AxesSubplot at 0x7f55653e5dd0>

In [29]:
df.cumsum().hist(figsize=(20, 8))


Out[29]:
array([[<matplotlib.axes.AxesSubplot object at 0x7f5565185cd0>,
        <matplotlib.axes.AxesSubplot object at 0x7f556517e110>],
       [<matplotlib.axes.AxesSubplot object at 0x7f55650cd490>,
        <matplotlib.axes.AxesSubplot object at 0x7f5564df01d0>]], dtype=object)

In [44]:
df = pd.DataFrame({
'B': np.random.randint(0, 100, 5),
'C': np.random.randint(50, 150, 5),
'D': np.random.randint(0, 100, 5),
}, index=list('ABCDE'))
# plt.figure()
df['B'].plot(kind='bar')
df.plot(kind='bar')
df.plot(kind='bar', stacked=True)
df.plot(kind='barh', stacked=True)


Out[44]:
<matplotlib.axes.AxesSubplot at 0x7f5564601fd0>

In [45]:
df.plot(kind='hist')


Out[45]:
<matplotlib.axes.AxesSubplot at 0x7f55649e10d0>

In [47]:
df = pd.DataFrame(np.random.rand(10, 5), columns=list('ABCDE'))
display_html(df)
df.plot(kind='box')


A B C D E
0 0.861996 0.854195 0.169300 0.163941 0.383768
1 0.086928 0.274264 0.918127 0.873760 0.840835
2 0.248209 0.963460 0.145096 0.654023 0.579490
3 0.437722 0.161625 0.444892 0.065964 0.329682
4 0.128290 0.105836 0.315159 0.714923 0.473313
5 0.808679 0.970344 0.937924 0.406341 0.173013
6 0.114899 0.359807 0.410558 0.225799 0.141491
7 0.081415 0.796789 0.621102 0.789584 0.305951
8 0.141955 0.072683 0.246083 0.321504 0.832740
9 0.466192 0.674512 0.252660 0.106329 0.401946
Out[47]:
<matplotlib.axes.AxesSubplot at 0x7f5564507150>

In [57]:
df = pd.DataFrame(np.random.rand(10,3), columns=['Col1', 'Col2', 'Col3'] )
df['X'] = pd.Series(['A','A','A','A','A','B','B','B','B','B'])
df['Y'] = pd.Series(['A','B','A','B','A','B','A','B','A','B'])
display_html(df)
df.boxplot(column=['Col1', 'Col2'], by=['X', 'Y'], figsize=(20, 5))


Col1 Col2 Col3 X Y
0 0.763797 0.760341 0.905555 A A
1 0.105471 0.878515 0.746531 A B
2 0.876214 0.471943 0.663997 A A
3 0.704899 0.994817 0.547028 A B
4 0.293411 0.676280 0.508604 A A
5 0.118772 0.576163 0.756079 B B
6 0.259740 0.085938 0.551347 B A
7 0.332336 0.495598 0.108375 B B
8 0.782794 0.268654 0.190788 B A
9 0.996578 0.176642 0.735227 B B
Out[57]:
array([<matplotlib.axes.AxesSubplot object at 0x7f5563d96390>,
       <matplotlib.axes.AxesSubplot object at 0x7f5563ca83d0>], dtype=object)

In [72]:
df = pd.DataFrame(np.random.rand(50, 4), columns=['a', 'b', 'c', 'd'])
ax = df.plot(kind='scatter', x='a', y='d', figsize=(8, 8), label='group 1', color='green')
df.plot(kind='scatter', x='b', y='c', figsize=(8, 8), ax=ax, label='group 2', color='red')
df.plot(kind='scatter', x='a', y='b', c='c', figsize=(11, 8), color='purple')
df.plot(kind='scatter', x='a', y='b', s=df['c'] * 200, figsize=(8, 8))


Out[72]:
<matplotlib.axes.AxesSubplot at 0x7f55631d2250>

In [85]:
df = pd.DataFrame(np.random.randn(1000, 2), columns=['a', 'b'])
df['b'] = df['b'] + np.arange(1000)
df.plot(kind='hexbin', x='a', y='b', gridsize=10)
df.plot(kind='hexbin', x='a', y='b', gridsize=50)


Out[85]:
<matplotlib.axes.AxesSubplot at 0x7f5561109e90>

In [93]:
df = pd.DataFrame(np.random.randn(1000, 2), columns=['a', 'b'])
df['b'] = df['b'] + np.arange(1000)
df['z'] = np.random.uniform(0, 3, 1000)
df.plot(kind='hexbin', x='a', y='b', C='z', reduce_C_function=np.mean, gridsize=25, figsize=(10, 5))
df.plot(kind='hexbin', x='a', y='b', C='z', reduce_C_function=np.max, gridsize=25, figsize=(10, 5))


Out[93]:
<matplotlib.axes.AxesSubplot at 0x7f5560cc7810>

Pie plot


In [108]:
df = pd.DataFrame(np.random.rand(5, 2), columns=['X', 'Y'])
# display_html(df)
df.plot(kind='pie', subplots=True, figsize=(12, 6), labels=list('ABCDE'), autopct='%.1f%%', fontsize=16, table=True)


Out[108]:
array([<matplotlib.axes.AxesSubplot object at 0x7f55601a8f10>,
       <matplotlib.axes.AxesSubplot object at 0x7f556003f0d0>], dtype=object)

Plotting tools


In [ ]: