我有一个让用户点击图表上的数据点的功能。然后on_pick函数将生成一个新的数字。我现在遇到的问题是,每次点击数据时都会创建一个新的图形窗口。有没有办法重用相同的图窗口并在该窗口上创建新的图? 我试图在plotpot函数(生成绘图的函数)之前放置图(2),希望告诉python始终使用该窗口。但相反,它产生了一个空图(2)并将我的情节放在图(3)中,依此类推。
谢谢你的建议。
嘉俊
def plotpot(data2d, sigma=.1, Nx=40, Ny=40):
# ensure the data sending for plotpot is always in 2d.
potmap = zeros((Nx, Ny))
for i in range(Nx):
for j in range(Ny):
x = float(i)/Nx-0.5
y = float(j)/Ny-0.5
potmap[j,i] = potential_ds(data2d, np.array([x,y]), sigma)
matshow(potmap, cmap=cm.gray, extent=(-0.5,0.5,0.5,-0.5))
plot(data[:,0], data[:,1], ".")
axis([-0.5, 0.5, -0.5, 0.5])
def on_pick(event):
global data, vel, sigma
vel = rand(dim); vel /= norm(vel)/1
artist = event.artist
ind = event.ind
indNp = np.array(ind)
pos = np.array(data[indNp[0],:])
# a Function to calculate trajectory
trj = PTSM(pos, data, vel, sigma = sigma, dt = dt, r = .9995, Nsamp = 15000)
# create a potential plot
plotpot(data[:, 0:2], sigma)
plot(trj[:,1], trj[:,2], "-", lw=0.7, c = "green")