Thursday, October 18, 2012

Plot binned mean and mean plus/minus std using python

python scripts to plot mean and mean plus/minus std for data in time bin:


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
view raw binPlots.py hosted with ❤ by GitHub

No comments: