Visualizing with pandas


In [49]:
%matplotlib inline

# standard
import matplotlib.pyplot as plt
import pandas as pd
import numpy as np

# I've got style,
# miles and miles
import seaborn as sns
sns.set()
sns.set_context('notebook', font_scale=1.5)
cp = sns.color_palette()


Thing 1: Line Chart (with many lines)


In [2]:
ts = pd.read_csv('data/ts.csv')

# casting to datetime is important for
# ensuring plots "just work"
ts = ts.assign(dt = pd.to_datetime(ts.dt))
ts.head()
Out[2]:
dt kind value
0 2000-01-01 A 1.442521
1 2000-01-02 A 1.981290
2 2000-01-03 A 1.586494
3 2000-01-04 A 1.378969
4 2000-01-05 A -0.277937
In [3]:
# in matplotlib-land, the notion of a "tidy"
# dataframe matters not
dfp = ts.pivot(index='dt', columns='kind', values='value')
dfp.head()
Out[3]:
kind A B C D
dt
2000-01-01 1.442521 1.808741 0.437415 0.096980
2000-01-02 1.981290 2.277020 0.706127 -1.523108
2000-01-03 1.586494 3.474392 1.358063 -3.100735
2000-01-04 1.378969 2.906132 0.262223 -2.660599
2000-01-05 -0.277937 3.489553 0.796743 -3.417402
In [6]:
fig, ax = plt.subplots(1, 1,
                       figsize=(7.5, 5))

dfp.plot(ax=ax)

ax.set(xlabel='Date',
       ylabel='Value',
       title='Random Timeseries')
    
ax.legend(loc=2)
fig.autofmt_xdate()


Thing 2: Scatter


In [7]:
df = pd.read_csv('data/iris.csv')
df.head()
Out[7]:
petalLength petalWidth sepalLength sepalWidth species
0 1.4 0.2 5.1 3.5 setosa
1 1.4 0.2 4.9 3.0 setosa
2 1.3 0.2 4.7 3.2 setosa
3 1.5 0.2 4.6 3.1 setosa
4 1.4 0.2 5.0 3.6 setosa
In [22]:
fig, ax = plt.subplots(1, 1, figsize=(7.5, 7.5))

for i, s in enumerate(df.species.unique()):
    df[df.species == s].plot.scatter(
        'petalLength', 'petalWidth',
        c=cp[i], label=s, ax=ax
    )

ax.set(xlabel='Petal Length',
       ylabel='Petal Width',
       title='Petal Width v. Length -- by Species')

ax.legend(loc=2)
Out[22]:
<matplotlib.legend.Legend at 0x11b2bba50>


Thing 3: Trellising the Above


In [39]:
dfp.plot(subplots=True, layout=(2, 2), figsize=(10, 10),
         title='Random Timeseries -- Value over Time')
Out[39]:
array([[<matplotlib.axes._subplots.AxesSubplot object at 0x125f73450>,
        <matplotlib.axes._subplots.AxesSubplot object at 0x1266fc950>],
       [<matplotlib.axes._subplots.AxesSubplot object at 0x12677f950>,
        <matplotlib.axes._subplots.AxesSubplot object at 0x1267e1910>]], dtype=object)
In [40]:
tmp_n = df.shape[0] - df.shape[0]/2

df['random_factor'] = np.random.permutation(['A'] * tmp_n + ['B'] * (df.shape[0] - tmp_n))
df.head()
Out[40]:
petalLength petalWidth sepalLength sepalWidth species random_factor
0 1.4 0.2 5.1 3.5 setosa A
1 1.4 0.2 4.9 3.0 setosa A
2 1.3 0.2 4.7 3.2 setosa B
3 1.5 0.2 4.6 3.1 setosa B
4 1.4 0.2 5.0 3.6 setosa A


Thing 4: Visualizing Distributions (Boxplot and Histogram)


In [53]:
fig, ax = plt.subplots(1, 1, figsize=(10, 10))

df.boxplot(column='petalWidth', by='species', ax=ax)
Out[53]:
<matplotlib.axes._subplots.AxesSubplot at 0x127666690>
In [54]:
sns.set_context('notebook', font_scale=1.25)

fig, ax = plt.subplots(1, 1, figsize=(10, 10))

df.boxplot(column='petalWidth', by=['species', 'random_factor'], ax=ax)
Out[54]:
<matplotlib.axes._subplots.AxesSubplot at 0x1296ecd10>
In [62]:
sns.set_context('notebook', font_scale=1.5)

fig, ax = plt.subplots(1, 1, figsize=(10, 10))

df.hist(column='petalWidth', by='species', grid=None, ax=ax)
Out[62]:
array([[<matplotlib.axes._subplots.AxesSubplot object at 0x12c5ac1d0>,
        <matplotlib.axes._subplots.AxesSubplot object at 0x12ca53a10>],
       [<matplotlib.axes._subplots.AxesSubplot object at 0x12cab53d0>,
        <matplotlib.axes._subplots.AxesSubplot object at 0x12cc37850>]], dtype=object)


Thing 5: Bar Chart


In [69]:
df = pd.read_csv('data/titanic.csv')
df.head()
Out[69]:
survived pclass sex age sibsp parch fare embarked class who adult_male deck embark_town alive alone
0 0 3 male 22.0 1 0 7.2500 S Third man True NaN Southampton no False
1 1 1 female 38.0 1 0 71.2833 C First woman False C Cherbourg yes False
2 1 3 female 26.0 0 0 7.9250 S Third woman False NaN Southampton yes True
3 1 1 female 35.0 1 0 53.1000 S First woman False C Southampton yes False
4 0 3 male 35.0 0 0 8.0500 S Third man True NaN Southampton no True
In [70]:
dfg = df.groupby(['survived', 'pclass']).agg({'fare': 'mean'})
dfg
Out[70]:
fare
survived pclass
0 1 64.684008
2 19.412328
3 13.669364
1 1 95.608029
2 22.055700
3 13.694887
In [77]:
fig, ax = plt.subplots(1, 1, figsize=(12.5, 7))

dfg.reset_index().\
    pivot(index='pclass',
          columns='survived',
          values='fare').plot.bar(ax=ax)

ax.set(xlabel='Class',
       ylabel='Fare',
       title='Fare by survival and class')
Out[77]:
[<matplotlib.text.Text at 0x12e5e5f90>,
 <matplotlib.text.Text at 0x11744b350>,
 <matplotlib.text.Text at 0x12e619450>]