bgbg bgbg - 2 months ago 13
Python Question

matplotlib analog of R's `pairs`

R has a useful function

that provides nice matrix of plots of pairwise connections between variables in a data set. The resulting plot looks similar to the following figure, copied from this blog post:


Is there any ready to use function based on python's matplolib? I have searched its gallery, but couldn't find anything that resembles what I need. Technically, this should be a simple task, but proper handling of all the possible cases, labels, titles, etc is very tedious.

UPDATE see below my answer with a quick and dirty approximation.

bmu bmu

Pandas has a built in function scatter_matrix (source code) which is something like this.

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

df = pd.DataFrame(np.random.randn(1000, 4), columns=['A','B','C','D'])
axes =, alpha=0.2)


However it is pandas specific (but could be used as a starting point).

There are some more R like plots in pandas. Have a look at the docs.