I have an embedded live matplotlib graph that updates every minute in a tkinter window. I want the graph to continue to update even when the window is closed. Also, when a person clicks a button, it will open the same tkinter window in it's updated state. Here is the code:
import matplotlib.pyplot as plt
import matplotlib.animation as animation
from matplotlib.figure import Figure
from matplotlib.backends.backend_tkagg import (FigureCanvasTkAgg, NavigationToolbar2Tk)
import pytz
root=Tk()
root.title("Live Graph")
nextdate = datetime.datetime.today() + datetime.timedelta(days=1)
todaydate = date.today()
def plotcom():
fig = plt.figure(figsize = (8,6))
ax1 = fig.add_subplot(1, 1, 1)
var5 = "TSLA"
def animate(i):
tickerData = yf.Ticker(var5)
dsp = tickerData.history(interval='1m', start = todaydate, end=nextdate)
dspclose = dsp["Close"]
ax1.set_xlabel("Time")
date_form = DateFormatter("%H-%M-%S", tz=pytz.timezone("America/New_York"))
ax1.xaxis.set_major_formatter(date_form)
ax1.set_ylabel("Price")
ax1.set_title(var5+" - Live Graph")
ax1.legend()
canvas = FigureCanvasTkAgg(fig, master = root)
canvas.draw()
canvas.get_tk_widget().pack()
canvas.get_tk_widget().place(x=10, y=240)
ani = animation.FuncAnimation(fig, animate, interval = 60000)
plt.show()
x = Button(root, text="Plot", command=plotcom)
x.pack()
x.place(x=300, y=0)
root.mainloop()