我创建了一个基于多个数组(x、y 和 a,其中 x& y 是坐标,a 是温度)的温度图。我正在尝试向它添加一个滑块并继续
TypeError: Input z must be a 2D array.
当我尝试使用滑块时。初始图是正确的。据我所知,我对原始和更新的输入是相同的,除了一个变量(数组的行)。
a=np.array(Matrix) #contains all temperatures, approx. 2000 points*4 sensors
a0=a[0] #first set of temperatures
x=np.array([0,0,2,2])
y=np.array([0,2,0,2])
fig, ax = plt.subplots()
plt.subplots_adjust(left=0.25, bottom=0.25)
xi, yi = np.mgrid[x.min():x.max():500j, y.min():y.max():500j]
zi = griddata(x,y,a0,xi,yi, interp='linear')
CS = plt.contourf(xi,yi,zi,50,cmap='hsv_r', vmax=M, vmin=m)
plt.colorbar(CS)
plt.scatter(x,y,marker='o',c='none',s=128, lw=1,zorder=10)
ax.scatter(x, y, c='none', s=64, lw=0)
ax.set(xlabel='X', ylabel='Y', title='Title')
axsigma = p.axes([0.25, 0.10, 0.65, 0.03], axisbg='#A5D3FF')
slider1 = Slider (axsigma, 'Datapoint',0, datapoints-2, valinit=0, valfmt='%0.f', dragging=True, fc='#1E90FF')
canvas1=axsigma.figure.canvas
def update (val):
val=int(val)
new=a[val]
canvas1.blit(axsigma.bbox)
ax.scatter(x, y, c='none', s=128, lw=.1)
zi = griddata(x,y,new,xi,yi, interp='linear')
CS = plt.contourf(xi,yi,zi,50,cmap='hsv', vmax=M, vmin=m)
slider1.on_changed(update)
任何帮助将非常感激!
编辑
添加
a=np.array([[6.7, 6.5, 5.9, 6.0], [4.7, 4.7, 4.2, 4.2], [5.1, 5.2, 5.5, 4.9]])
M=a.max()
m=a.min()
datapoints=len(a)
应该提供足够的信息来运行。通过更改new
为zi
in CS
within def update
,它消除了错误,但滑块仍然不调整图形