首页 文章

更新Tkinter Matplotlib图

提问于
浏览
1

我试图在用户点击屏幕时重绘单个图 . 目前,该图是在第一次点击期间绘制的 . 之后,新的绘图将附加到画布上 . 我想做的是“删除”或“清除”第一个图并重新绘制或用新数据更新它 .

这是负责此特定情节绘制的部分:

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' .

1 回答

  • 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])
        ...
    

相关问题