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!