Pandas Visualization

In [1]:
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt

%matplotlib notebook
In [2]:
# see the pre-defined styles provided.
plt.style.available
Out[2]:
['grayscale',
 'seaborn-poster',
 'seaborn-talk',
 'seaborn-darkgrid',
 'ggplot',
 'dark_background',
 'seaborn',
 'seaborn-dark',
 'seaborn-whitegrid',
 'seaborn-colorblind',
 'seaborn-dark-palette',
 'seaborn-pastel',
 'fivethirtyeight',
 'seaborn-ticks',
 'seaborn-bright',
 'classic',
 'seaborn-white',
 'seaborn-notebook',
 'seaborn-muted',
 'seaborn-paper',
 'bmh',
 'seaborn-deep']
In [3]:
# use the 'seaborn-colorblind' style
plt.style.use('seaborn-colorblind')

DataFrame.plot

In [4]:
np.random.seed(123)

df = pd.DataFrame({'A': np.random.randn(365).cumsum(0), 
                   'B': np.random.randn(365).cumsum(0) + 20,
                   'C': np.random.randn(365).cumsum(0) - 20}, 
                  index=pd.date_range('1/1/2017', periods=365))
df.head()
Out[4]:
A B C
2017-01-01 -1.085631 20.059291 -20.230904
2017-01-02 -0.088285 21.803332 -16.659325
2017-01-03 0.194693 20.835588 -17.055481
2017-01-04 -1.311601 21.255156 -17.093802
2017-01-05 -1.890202 21.462083 -19.518638
In [5]:
df.plot(); # add a semi-colon to the end of the plotting call to suppress unwanted output

We can select which plot we want to use by passing it into the 'kind' parameter.

In [6]:
df.plot('A','B', kind = 'scatter');

You can also choose the plot kind by using the DataFrame.plot.kind methods instead of providing the kind keyword argument.

kind :

  • 'line' : line plot (default)
  • 'bar' : vertical bar plot
  • 'barh' : horizontal bar plot
  • 'hist' : histogram
  • 'box' : boxplot
  • 'kde' : Kernel Density Estimation plot
  • 'density' : same as 'kde'
  • 'area' : area plot
  • 'pie' : pie plot
  • 'scatter' : scatter plot
  • 'hexbin' : hexbin plot
In [7]:
# create a scatter plot of columns 'A' and 'C', with changing color (c) and size (s) based on column 'B'
df.plot.scatter('A', 'C', c='B', s=df['B'], colormap='viridis')
Out[7]:
<matplotlib.axes._subplots.AxesSubplot at 0x7f121cc39048>
In [8]:
ax = df.plot.scatter('A', 'C', c='B', s=df['B'], colormap='viridis')
ax.set_aspect('equal')
In [9]:
df.plot.box();
In [10]:
df.plot.hist(alpha=0.7);

Kernel density estimation plots are useful for deriving a smooth continuous function from a given sample.

In [11]:
df.plot.kde();

pandas.tools.plotting

In [12]:
iris = pd.read_csv('iris.csv')
iris.head()
Out[12]:
SepalLength SepalWidth PetalLength PetalWidth Name
0 5.1 3.5 1.4 0.2 Iris-setosa
1 4.9 3.0 1.4 0.2 Iris-setosa
2 4.7 3.2 1.3 0.2 Iris-setosa
3 4.6 3.1 1.5 0.2 Iris-setosa
4 5.0 3.6 1.4 0.2 Iris-setosa
In [13]:
pd.tools.plotting.scatter_matrix(iris);
In [14]:
plt.figure()
pd.tools.plotting.parallel_coordinates(iris, 'Name');