In [1]:
import numpy as np
import pandas as pd
from altair import api, html


def pairgrid_transform(data, rows, cols=None):
    """
    Transform a dataframe to allow vega to produce a PairGrid
    
    """
    if cols is None:
        cols = rows
      
    M1 = len(rows)
    M2 = len(cols)
    N = len(data)
    
    data = pd.concat(M1 * M2 * [data])
    i = np.arange(N * M1 * M2)
    irow = np.arange(N * M1 * M2) // N % M1
    icol = np.arange(N * M1 * M2) // N // M1
    
    data['x'] = data[cols].values[i, icol]
    data['y'] = data[rows].values[i, irow]
    data['row'] = rows.values[irow]
    data['col'] = cols.values[icol]
    
    return data

In [2]:
import seaborn as sns
iris = sns.load_dataset('iris')
iris.head()


Out[2]:
sepal_length sepal_width petal_length petal_width species
0 5.1 3.5 1.4 0.2 setosa
1 4.9 3.0 1.4 0.2 setosa
2 4.7 3.2 1.3 0.2 setosa
3 4.6 3.1 1.5 0.2 setosa
4 5.0 3.6 1.4 0.2 setosa

In [3]:
data = pairgrid_transform(iris, iris.columns[:4])
data.head()


Out[3]:
sepal_length sepal_width petal_length petal_width species x y row col
0 5.1 3.5 1.4 0.2 setosa 5.1 5.1 sepal_length sepal_length
1 4.9 3.0 1.4 0.2 setosa 4.9 4.9 sepal_length sepal_length
2 4.7 3.2 1.3 0.2 setosa 4.7 4.7 sepal_length sepal_length
3 4.6 3.1 1.5 0.2 setosa 4.6 4.6 sepal_length sepal_length
4 5.0 3.6 1.4 0.2 setosa 5.0 5.0 sepal_length sepal_length

In [4]:
from IPython.display import HTML

spec = api.Viz(data).encode(x='x', y='y', row='row', col='col', color='species').point()
html_out = html.render(spec)
HTML(html_out)


/Users/jakevdp/anaconda/envs/py3k/lib/python3.3/site-packages/pandas/core/internals.py:956: FutureWarning: comparison to `None` will result in an elementwise object comparison in the future.
  return self._try_coerce_result(func(values, other))
Out[4]: