简体   繁体   English

如何用新的绘图或绘图网格替换matplotlib图中的先前绘图?

[英]How to replace previous plots in a matplotlib figure with new plots or grid of plots?

I am using matplotlib and create a figure adding a plot. 我正在使用matplotlib并创建一个添加绘图的图形。 How can I replace the plot by a new one or a new grid of plots? 如何用新的地块或新的地块网格替换地块? In my present code I create an axes with the menu dosomething() then add other red lines with the menu dosomethingelse() over the same axes. 在我目前的代码中,我使用菜单dosomething()创建了一个轴,然后在同一轴上通过菜单dosomethingelse()添加了其他红线。 Everytime I dosomething(), a new figure is appended bellow the current one, but I actually want to replace the current axes by a new one in the same figure. 每次我执行somesomething()时,都会在当前图形之后附加一个新图形,但实际上我想用同一图形中的新图形替换当前轴。 How can I do that? 我怎样才能做到这一点?

import numpy as np
from Tkinter import *
from matplotlib.figure import Figure
from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg

class Test1:   
    def __init__(self, windows, data, axes):
        self.windows = windows
        self.data = data
        self.figure = axes.figure
        self.axes = axes
        self.im = self.axes.plot(data)

def dosomething():
    global test1
    global fig
    fig = Figure(figsize=(12, 4))
    axes = fig.add_subplot(111)
    canvas = FigureCanvasTkAgg(fig, master=windows)
    canvas.get_tk_widget().pack()
    data=np.arange(100)
    test1=Test1(windows, data, axes)

def dosomethingelse():
    global test1
    test1.axes.plot(np.arange(100)+10*(np.random.rand(100)-0.5),'-r')
    test1.figure.canvas.show()

windows = Tk()
menubar = Menu(windows)
filemenu = Menu(menubar, tearoff=0)
filemenu.add_command(label="Do something", command=dosomething)
filemenu.add_command(label="Do somethingelse", command=dosomethingelse)
menubar.add_cascade(label="Tool", menu=filemenu)

windows.config(menu=menubar)

windows.mainloop()

This doesn't looks like a very standard way of doing tkinter. 这看起来不是执行tkinter的非常标准的方法。 At least it's not very clear to me what you're trying to achieve. 至少对我来说,你要达到的目标不是很清楚。 You have the following line in your dosomething: 您的工作中包含以下内容:

test1=Test1(windows, data, axes)

which is what is producing your new window every time you run it. 这就是您每次运行新窗口时产生的结果。 Also, there's no need for global variables, when you're inside a class. 此外,当您在类中时,也不需要全局变量。 Just use self.variable = ..., and the variable will be available throughout your class and to objects that you pass the class to. 只需使用self.variable = ...,变量将在整个类中以及传递给类的对象中可用。

I haven't tried this, but perhaps something like this: 我没有尝试过,但是也许是这样的:

def dosomething():
    try: 
        self.canvas.get_tk_widget().destroy()
    except:
        pass        
    fig = Figure(figsize=(12, 4))
    axes = fig.add_subplot(111)
    self.canvas = FigureCanvasTkAgg(fig, master=windows)
    self.canvas.get_tk_widget().pack()
    data=np.arange(100)  # not sure what this is for

def dosomethingelse():
    try: 
        self.canvas.get_tk_widget().destroy()
    except:
        pass 
    fig = Figure(figsize=(12, 4))
    fig.plot(np.arange(100)+10*(np.random.rand(100)-0.5),'-r')
    self.canvas = FigureCanvasTkAgg(fig, master=windows)
    self.canvas.get_tk_widget().pack()        

I am posting the full code which worked for me after the answer of @DrXorile 我要在@DrXorile回答后发布对我有用的完整代码

import numpy as np
from Tkinter import *
from matplotlib.figure import Figure
from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg

class Test1:

    def __init__(self, data):
        self.data = data

    def dosomething(self):
        try: 
            self.canvas.get_tk_widget().destroy()
        except:
            pass        
        self.figure = Figure(figsize=(12, 4))
        self.axes = self.figure.add_subplot(111)
        self.im = self.axes.plot(data)
        self.canvas = FigureCanvasTkAgg(self.figure, master=windows)
        self.canvas.get_tk_widget().pack()

    def dosomethingelse(self):
        self.axes.plot(np.arange(100)+10*(np.random.rand(100)-0.5),'-r')
        self.figure.canvas.show()

data=np.arange(100)  # data to plot
test1=Test1(data)

windows = Tk()
menubar = Menu(windows)
filemenu = Menu(menubar, tearoff=0)
filemenu.add_command(label="Do something", command=test1.dosomething)
filemenu.add_command(label="Do somethingelse", command=test1.dosomethingelse)
menubar.add_cascade(label="Tool", menu=filemenu)

windows.config(menu=menubar)

windows.mainloop()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM