2013-07-12 66 views
1

我已經基於幾個陣列(x,y,& a,其中x & y是座標,a是溫度)創建了溫度圖。我試圖滑動條添加到它,並不斷得到使用Matplotlib滑塊更改表面

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) 

應該提供足夠的信息來運行。 通過內def updateCS改變newzi,它擺脫了錯誤,但滑塊仍然沒有調整圖形

回答

1

既然你沒有提供一個可運行的例子,我只能猜測。你發佈的一個問題是在update函數中你想要實際更新你繪製的藝術家,而不是額外繪製一個。沿

... 
scat = ax.scatter(x, y, c='none', s=64, lw=0) 
... 

東西線,然後

def update(val): 
    ... 
    scat.set_paths(...) # update the artist 
    ... 
    ax.draw() # redraw the axes 
+0

我剛剛更新的代碼,以便它現在應該運行 – user2386081

+0

就像我說的,你應該實際更新藝術家(路徑集合,萬一'scatter')。這不是你的代碼在做什麼:你試圖在舊的情節之上繪製一個新的東西。 –