我有一组N
从一个点开始的随机路径X
。我有一个 Slider 可以更改初始点X
,从而生成新N
路径。我希望能够更新路径,但set_ydata
只接受一维数组。目前我正在清除轴并在每次更新时进行绘图,这不是很有效。有没有内置的方法matplotlib
呢?
xJ = arange(-10,10,0.1)
psinaive = zeros((xJ.shape[0]))
uapprox = zeros((xJ.shape[0],Nt))
wplot = []
wcondplot = []
for i,x in enumerate(xJ):
WJ = sqrt(dt)*np.random.randn(Ntraj,Nt)
WJ[:,0] = x
WJ = np.cumsum(WJ,1)
wplot.append(WJ)
cond = V(WJ,limits)[0]
wcondplot.append(WJ[cond,:])
wa = 1.0/WJ.shape[0]*exp(-phi(WJ[:,-1],alpha)/lmbda)
psinaive[i] = sum(wa[cond])
uapprox[i,:] = 1.0/psinaive[i]*np.dot(wa[cond],WJ[cond,:]).flatten()
if i % 10 == 0:
print '..%.1f'%x,
J = -lmbda*log(psinaive)
#plot results
ax1=subplot(221)
plot(xJ,J)
plot(xJ,Jl(xJ,ti,tf,alpha,R,v,t1,limits))
title('$J(x,t)$')
plt.axvline(x=-10)
subplot(222)
plot(xJ,uapprox[:,0])
ax3 = subplot(223)
plot(t,wplot[0].T,alpha=0.2)
title('%d paths'%Ntraj)
ylim((-15,15))
ymin,ymax = ylim()
plt.axvline(x=t1, ymin=(d-ymin)/(ymax-ymin), linewidth=2, color='k')
plt.axvline(x=t1, ymin=(b-ymin)/(ymax-ymin), ymax = (c-ymin)/(ymax-ymin), linewidth=2, color='k')
plt.axvline(x=t1, ymax=(a-ymin)/(ymax-ymin), linewidth=2, color='k')
ax4 = subplot(224)
ax4.set_title('No alive paths')
if len(wcondplot[0])>0:
ax4.plot(t,wcondplot[0].T,alpha=0.2)
ax4.set_title('%d alive from %d paths'%(len(wcondplot[0]),Ntraj))
ylim((-15,15))
ymin,ymax = ylim()
plt.axvline(x=t1, ymin=(d-ymin)/(ymax-ymin), linewidth=2, color='k')
plt.axvline(x=t1, ymin=(b-ymin)/(ymax-ymin), ymax = (c-ymin)/(ymax-ymin), linewidth=2, color='k')
plt.axvline(x=t1, ymax=(a-ymin)/(ymax-ymin), linewidth=2, color='k')
subplots_adjust(0.15,0.25)
axsx = axes([0.15,0.1,0.75,0.1])
slx = Slider(axsx,'x',0,len(xJ),0,valfmt='%.0f')
def updatex(val):
x = int(val)
ax1.cla()
ax1.plot(xJ,J)
ax1.plot(xJ,Jl(xJ,ti,tf,alpha,R,v,t1,limits))
ax1.set_title('$J(x,t)$')
ax1.axvline(x=xJ[x])
ax3.cla()
ax3.plot(t,wplot[x].T,alpha=0.2)
ax3.set_title('%d paths'%Ntraj)
ax3.set_ylim((-15,15))
ymin,ymax = ax3.get_ylim()
ax3.axvline(x=t1, ymin=(d-ymin)/(ymax-ymin), linewidth=2, color='k')
ax3.axvline(x=t1, ymin=(b-ymin)/(ymax-ymin), ymax = (c-ymin)/(ymax-ymin), linewidth=2, color='k')
ax3.axvline(x=t1, ymax=(a-ymin)/(ymax-ymin), linewidth=2, color='k')
ax4.cla()
ax4.set_title('No alive paths')
if len(wcondplot[x])>0:
ax4.plot(t,wcondplot[x].T,alpha=0.4)
ax4.set_title('%d alive from %d paths'%(len(wcondplot[x]),Ntraj))
ax4.set_ylim((-15,15))
ymin,ymax = ax4.get_ylim()
ax4.axvline(x=t1, ymin=(d-ymin)/(ymax-ymin), linewidth=2, color='k')
ax4.axvline(x=t1, ymin=(b-ymin)/(ymax-ymin), ymax = (c-ymin)/(ymax-ymin), linewidth=2, color='k')
ax4.axvline(x=t1, ymax=(a-ymin)/(ymax-ymin), linewidth=2, color='k')
draw()
slx.on_changed(updatex)
结果是: