Reputation: 3984
I tried to adapt Hans' answer in Matplotlib plot in Tkinter - every update adds new NavigationToolbar? to have 2 different plots in 2 different canvas.
I have a problem: I don't know how to refer to the two different figures.
Although fig1 is in canvas1 and fig2 in canvas2, the plt.plot function doesnt refer to them - and plt.figure() doesnt help. How can I plot one thing in a canvas and a different thing in the other one?
from Tkinter import Tk, Button
import numpy as np
import random
import matplotlib.pyplot as plt
from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg, NavigationToolbar2TkAgg
def plotthem():
plt.figure(1)
plt.clf()
x = np.arange(0.0,3.0,0.01)
y = np.sin(2*np.pi*x+random.random())
plt.plot(x,y)
plt.gcf().canvas.draw()
plt.figure(2)
plt.clf()
x = np.arange(0.0,3.0,0.01)
y = np.tan(2*np.pi*x+random.random())
plt.plot(x,y)
plt.gcf().canvas.draw()
root = Tk()
b = Button(root, text="Plot", command = plotthem)
b.grid(row=0, column=0)
# init figures
fig1 = plt.figure()
canvas1 = FigureCanvasTkAgg(fig1, master=root)
toolbar = NavigationToolbar2TkAgg(canvas1, root)
canvas1.get_tk_widget().grid(row=0,column=1)
toolbar.grid(row=1,column=1)
fig2 = plt.figure()
canvas2 = FigureCanvasTkAgg(fig2, master=root)
toolbar = NavigationToolbar2TkAgg(canvas2, root)
canvas2.get_tk_widget().grid(row=0,column=2)
toolbar.grid(row=1,column=2)
root.mainloop()
Upvotes: 2
Views: 3674
Reputation: 3984
ok, I figured it out...
You just have to use plt.gcf().canvas.draw()
each time, since gcf()
stands for get current figure, and working in tandem with plt.figure(
) it is able to update different figures in different canvas.
Upvotes: 1