I am using matplotlib and create a figure adding a plot. 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. 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. 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. 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.
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
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()
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.