1

When I try to graph something with matplotlib in a tkinter window, it simply won't show up. If I remove the tkinter specific part, and just do a basic plt.plot(...) and plt.show() it shows up in my regular text output field. However, in this case I would like it to show up in a tkinter window. I Believe its because I am running this on a mac (macOS 10.12.4), however I cannot figure out how to get it to show up in the tkinter window.

import numpy as np
import Tkinter as tk

import matplotlib
matplotlib.use('TkAgg')

from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg
import matplotlib.pyplot as plt

root = tk.Tk()

fig = plt.figure(1)
t = np.arange(0.0,3.0,0.01)
s = np.sin(np.pi*t)
plt.plot(t,s)

canvas = FigureCanvasTkAgg(fig, master=root)
plot_widget = canvas.get_tk_widget()

def update():
    s = np.cos(np.pi*t)
    plt.plot(t,s)
    plt.show()

plot_widget.grid(row=0, column=0)
tk.Button(root,text="Update",command=update).grid(row=1, column=0)
root.mainloop()

As I said, I believe its due to the fact that I am running on MacOS. Also FYI when I run this a blank tkinter window shows up with the update button, but no graph. The Graph DOES however show up in the regular text output but I want it in the tkinter window for a GUI. Please Help!

1 Answer 1

3

You need to update the figure that is already in the tk canvas, instead of showing a new one with plt.show(). In this case there is only one figure open, so plt.plot() will actually work (although it might fail in more complex scenarios). What remains is to redraw the canvas, fig.canvas.draw_idle() after plotting.

import numpy as np
import Tkinter as tk
from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg
import matplotlib.pyplot as plt

root = tk.Tk()

fig = plt.figure(1)
t = np.arange(0.0,3.0,0.01)
s = np.sin(np.pi*t)
plt.plot(t,s)

canvas = FigureCanvasTkAgg(fig, master=root)
plot_widget = canvas.get_tk_widget()

def update():
    s = np.cos(np.pi*t)
    plt.plot(t,s)
    fig.canvas.draw_idle()

plot_widget.grid(row=0, column=0)
tk.Button(root,text="Update",command=update).grid(row=1, column=0)
root.mainloop()

For the future and to be on the save side, try to avoid using pyplot when embedding matplotlib into GUIs. This prevents you from having issues like the one reported here.

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

root = tk.Tk()

fig = Figure()
ax = fig.add_subplot(111)
t = np.arange(0.0,3.0,0.01)
s = np.sin(np.pi*t)
ax.plot(t,s)

canvas = FigureCanvasTkAgg(fig, master=root)
plot_widget = canvas.get_tk_widget()

def update():
    s = np.cos(np.pi*t)
    ax.plot(t,s)
    fig.canvas.draw_idle()

plot_widget.grid(row=0, column=0)
tk.Button(root,text="Update",command=update).grid(row=1, column=0)
root.mainloop()
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.