2017-02-20 113 views
1

我试图在用户单击屏幕时重绘一个绘图。 当前,情节在第一次点击时绘制。之后,新的情节被追加到画布上。我想要做的是“删除”或“清除”第一个绘图并重新绘制或使用新数据对其进行更新。正在更新Tkinter Matplotlib图

这是部分负责此特定情节抽奖:

class AppGUI(Tk.Frame): 

    def __init__(self, parent): 
     self.parent = parent 
     self.initGUI() 
     self.plot() 


    def initGUI(self): 
     self.vf_frame = Tk.Frame(self.parent, bd=1, relief=Tk.SUNKEN) 
     self.vf_frame.pack(side=Tk.TOP, fill="both", expand=True) 

    def plotVF(self, u, v): 
      # Canvas of VF 
      m = np.sqrt(np.power(u, 2) + np.power(v, 2)) 

      xrange = np.linspace(0, u.shape[1], u.shape[1]); 
      yrange = np.linspace(0, u.shape[0], u.shape[0]); 

      x, y = np.meshgrid(xrange, yrange) 
      mag = np.hypot(u, v) 
      scale = 1 
      lw = scale * mag/mag.max() 

      f, ax = plt.subplots() 
      h = ax.streamplot(x, y, u, v, color=mag, linewidth=lw, density=3, arrowsize=1, norm=plt.Normalize(0, 70)) 
      ax.set_xlim(0, u.shape[1]) 
      ax.set_ylim(0, u.shape[0]) 
      ax.set_xticks([]) 
      ax.set_yticks([]) 
      #cbar = f.colorbar(h, cax=ax) 
      #cbar.ax.tick_params(labelsize=5) 

      c = FigureCanvasTkAgg(f, master=self.vf_frame) 
      c.show() 
      c.get_tk_widget().pack(side=Tk.LEFT, fill="both", expand=True) 

我必须做我的班fax属性来达到这个结果呢?为了清楚起见,plotVF通过其他方法更新。

PS:我不能同时显示带注释行的颜色条。它说'Streamplot' object has no attribute 'autoscale_None'

回答

0

您需要两个不同的功能,一个用于启动绘图,另一个用于更新它。

def initplot(self): 
    f, self.ax = plt.subplots() 
    c = FigureCanvasTkAgg(f, master=self.vf_frame) 
    c.show() 
    c.get_tk_widget().pack(side=Tk.LEFT, fill="both", expand=True) 

def update(self, u, v): 
    self.ax.clear() # clear the previous plot 
    ... 
    h = self.ax.streamplot(...) 
    self.ax.set_xlim(0, u.shape[1]) 
    ...