Skip to content

Instantly share code, notes, and snippets.

@ptweir
Created June 27, 2012 23:58
functions for plotting mean or mean and sem of numpy array
def binMeanPlot(X,Y,ax=None,numBins=8,xmin=None,xmax=None):
if xmin is None:
xmin = X.min()
if xmax is None:
xmax = X.max()
if ax is None:
fig = pylab.figure()
ax = fig.add_subplot(111)
bins = np.linspace(xmin,xmax,numBins+1)
XX = np.array([np.mean((bins[binInd], bins[binInd+1])) for binInd in range(numBins)])
YY = np.array([np.mean(Y[(X > bins[binInd]) & (X <= bins[binInd+1])]) for binInd in range(numBins)])
lineHandles = ax.plot(XX,YY)
return lineHandles[0], XX, YY
def binMeanStdPlot(X,Y,ax=None,numBins=8,xmin=None,xmax=None):
if xmin is None:
xmin = X.min()
if xmax is None:
xmax = X.max()
if ax is None:
fig = pylab.figure()
ax = fig.add_subplot(111)
bins = np.linspace(xmin,xmax,numBins+1)
XX = np.array([np.mean((bins[binInd], bins[binInd+1])) for binInd in range(numBins)])
YY = np.array([np.mean(Y[(X > bins[binInd]) & (X <= bins[binInd+1])]) for binInd in range(numBins)])
#XX[np.isnan(YY)]=np.nan
YYstd = np.array([np.std(Y[(X > bins[binInd]) & (X <= bins[binInd+1])]) for binInd in range(numBins)])
lineHandles = ax.plot(XX,YY)
patchHandle = ax.fill_between(XX[~np.isnan(YY)],YY[~np.isnan(YY)]-YYstd[~np.isnan(YY)],YY[~np.isnan(YY)]+YYstd[~np.isnan(YY)])
patchHandle.set_facecolor([.8, .8, .8])
patchHandle.set_edgecolor('none')
return lineHandles[0], patchHandle, XX, YY, YYstd
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment