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]:
In [3]:
data = pairgrid_transform(iris, iris.columns[:4])
data.head()
Out[3]:
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)
Out[4]: