Mar-09-2024, 04:48 PM
I am trying to plot a real time stock price graph on a tkinter frame using matplotlib's funcanimation and tkcanvas, however whenever I try and put it in the context of my project and use OOP/Classes, I get the following User Warning:
UserWarning: Animation was deleted without rendering anything. This is most likely not intended. To prevent deletion, assign the Animation to a variable, e.g.
warnings.warn(
Here is my code (I am fairy new to OOP in general so there may be some mistakes or better ways of doing things):
UserWarning: Animation was deleted without rendering anything. This is most likely not intended. To prevent deletion, assign the Animation to a variable, e.g.
anim
, that exists until you output the Animation using plt.show()
or anim.save()
.warnings.warn(
Here is my code (I am fairy new to OOP in general so there may be some mistakes or better ways of doing things):
import matplotlib.pyplot as plt from matplotlib.animation import FuncAnimation import pandas as pd import tkinter from matplotlib.backends.backend_tkagg import ( FigureCanvasTkAgg, NavigationToolbar2Tk) root = tkinter.Tk() class graph: def __init__(self): self.graph_frame = tkinter.Frame(master=root, width=500, height=300) def price_graph(self): fig = plt.Figure(dpi=70, constrained_layout=True) ax = fig.add_subplot(111) plt.rcParams.update({'font.size':10}) def animate(i): df = pd.read_csv('data.csv', skiprows=1) list_of_rows = [list(row) for row in df.values] xval = [] yval = [] for eachValue in list_of_rows: x,y = eachValue[0],eachValue[1] xval.append(int(x)) yval.append(float(y)) ax.clear() ax.plot(xval, yval, lw=1) return [ax] canvas = FigureCanvasTkAgg(fig, master=self.graph_frame) canvas.draw() canvas.get_tk_widget().pack() canvas._tkcanvas.pack() anim = FuncAnimation(fig, animate, interval=1000, blit=False) stock_graph = graph() stock_graph.price_graph() root.mainloop()Any help would be much appreciated.