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

时间:2015-10-22 21:43:06

标签: python matplotlib plot

我正在使用matplotlib并创建一个添加绘图的图形。如何用新的或新的网格替换图? 在我现在的代码中,我使用菜单dosomething()创建一个轴,然后在同一轴上添加其他红色线和菜单dosomethingelse()。 每次我dosomething(),一个新的数字附加在当前的一个,但我实际上想要在同一图中用新的轴替换当前的轴。我怎么能这样做?

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()

2 个答案:

答案 0 :(得分:1)

这看起来不像是一个非常标准的tkinter方式。至少我不太清楚你想要达到的目标。您的dosomething中有以下行:

test1=Test1(windows, data, axes)

每次运行时都会生成新窗口。此外,当您在课堂内时,不需要全局变量。只需使用self.variable = ...,该变量将在整个班级和您将该类传递给的对象中可用。

我没试过这个,但也许是这样的:

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()        

答案 1 :(得分:0)

我发布了@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()