我有一组N
随机路径,从X
点开始。我有一个Slider,可以更改初始点X
,从而生成新的N
路径。我希望能够更新路径,但set_ydata
只接受1D数组。目前我正在清理轴并在每次更新时进行绘图,这不是很有效。在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)
结果是:
答案 0 :(得分:1)
你可以使用这样一个事实,即如果数组包含nans,matplotlib将创建不同的段,尽管数组具有1维 -
x = (np.arange(5 * 4) % 4).reshape(5, 4) * 1.
x[x==3] = np.nan
y = x + (np.arange(5 * 4)/4).reshape(5,4)*1.
line2d = plt.plot(x.flatten(),y.flatten()) [0]
print x,y
> [[ 0., 1., 2., nan],
> [ 0., 1., 2., nan],
> [ 0., 1., 2., nan],
> [ 0., 1., 2., nan],
> [ 0., 1., 2., nan]]
> [[ 0. 1. 2. nan]
> [ 1. 2. 3. nan]
> [ 2. 3. 4. nan]
> [ 3. 4. 5. nan]
> [ 4. 5. 6. nan]]
然后您可以毫无问题地使用set_data()方法,例如:
line2d.set_data(y.flatten()+3)
答案 1 :(得分:0)
尝试使用set_data
,因为它可以节省你必须清除和重新绘制(你已经意识到非常慢)。用法示例:
fig = figure()
ax = fig.add_subplot(111)
p = ax.plot(x, y)
# you do something to your data and want to replot
p.set_data(x, y)
如果您可以发布代码,我们可以帮助您找到准确的解决方案。