Created
March 30, 2012 14:45
-
-
Save dfm/2252032 to your computer and use it in GitHub Desktop.
Triangle plots
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
__all__ = ["triplot"] | |
import itertools | |
import matplotlib.pyplot as pl | |
from matplotlib.ticker import MaxNLocator | |
import numpy as np | |
def triplot(samples, titles=None, sfactor=0.1): | |
""" | |
## Arguments | |
* `samples` (numpy.ndarray): The samples (nsamples, nvars). | |
* `titles` (list): A list of length nvars. | |
""" | |
nvars = samples.shape[-1] | |
if titles is None: | |
titles = [str(d) for d in range(nvars)] | |
ranges = [[np.min(samples[:,i])*(1-sfactor), | |
np.max(samples[:,i])*(1+sfactor)] for i in range(nvars)] | |
assert nvars == len(titles) | |
fig = pl.figure(figsize=(30, 30)) | |
for i, (yi,xi) in enumerate(itertools.product(np.arange(nvars), | |
np.arange(nvars))): | |
if xi <= yi: | |
x, y = samples[:,xi], samples[:,yi] | |
ax = fig.add_subplot(nvars, nvars, i+1) | |
if xi == yi: | |
ax.hist(x, 50, histtype="step", color="k") | |
ax.set_yticklabels([]) | |
ax.set_xlim(ranges[xi]) | |
else: | |
ax.plot(x, y, ".k") | |
ax.set_xlim(ranges[xi]) | |
ax.set_ylim(ranges[yi]) | |
ax.xaxis.set_major_locator(MaxNLocator(5)) | |
ax.yaxis.set_major_locator(MaxNLocator(5)) | |
if xi > 0: | |
ax.set_yticklabels([]) | |
else: | |
if yi > 0: | |
ax.set_ylabel(titles[yi]) | |
if yi < nvars-1: | |
ax.set_xticklabels([]) | |
else: | |
ax.set_xlabel(titles[xi]) | |
for t in ax.get_xticklabels(): | |
t.set_rotation(60) | |
if __name__ == "__main__": | |
nvars, nsamples = 10, 1000 | |
samples = np.random.randn(nvars*nsamples).reshape(nsamples, nvars) | |
titles = ["Variable %d"%i for i in range(nvars)] | |
triplot(samples, titles) | |
pl.savefig("triplot.png") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Hey Dan, maybe this will inspire you
def plotCorr(l, pars, plotfunc=None, lbls=None, _args, *_kwargs):
""" Plot correlation matrix between variables
inputs
l -- dictionary of variables (could be a Table)
pars -- parameters to use