Loading [MathJax]/extensions/TeX/AMSsymbols.js

Sunday, November 3, 2013

Slicing and histogramming

Some matplotlib kungfu, with fill_between and GridSpec that produces snazzy slice plots, with filled-in histograms.

import numpy as np
import pylab as plt
from numpy import random as random
import matplotlib.gridspec as gridspec
N = 10000
x,y = random.normal(0,1,N),random.normal(0,3,N)
plt.figure()
aspectRatio = 6
gs = gridspec.GridSpec(aspectRatio,aspectRatio)
ax1 = plt.subplot(gs[0,:-1])
ax2 = plt.subplot(gs[1:,:-1])
ax3 = plt.subplot(gs[1:,-1])
plt.subplots_adjust(wspace=0.02, hspace=0.02)
ax1.xaxis.set_visible(False)
ax1.yaxis.set_visible(False)
ax3.xaxis.set_visible(False)
ax3.yaxis.set_visible(False)
binSize = 50
a,b = np.histogram(y,len(y)/binSize)
c,d = np.histogram(x,len(x)/binSize)
ax1.plot(d[:-1],c,'b',linewidth=2)
ax1.fill_between(d[:-1],c,color='blue',alpha='0.2')
ax2.plot(x,y,'b.',markersize=4)
ax3.plot(a,b[:-1],'b',linewidth=2)
ax3.fill_between(a,b[:-1],color='blue',alpha='0.2')