Python Forum
[Tkinter] Tkinter Matplotlib Animation Graph not rendering
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[Tkinter] Tkinter Matplotlib Animation Graph not rendering
#1
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. 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.
Reply


Messages In This Thread
Tkinter Matplotlib Animation Graph not rendering - by dimidgen - Mar-09-2024, 04:48 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Interaction between Matplotlib window, Python prompt and TKinter window NorbertMoussy 3 651 Mar-17-2024, 09:37 AM
Last Post: deanhystad
  [Tkinter] cutomtkinter matplotlib no x,y - xaxis and x,y - labels-> only graph and grid visible dduric 0 360 Feb-20-2024, 07:09 PM
Last Post: dduric
  How to use rangesliders feature from matplotlib in tkinter? pymn 2 3,041 Feb-28-2022, 05:06 PM
Last Post: pymn
  [PyQt] Refresh x-labels in matplotlib animation widget JohnT 5 3,845 Apr-23-2021, 07:40 PM
Last Post: JohnT
  Tkinter Matplotlib Nepo 1 2,516 Sep-27-2020, 10:20 AM
Last Post: Gribouillis
  Tkinter & matplotlib PillyChepper 9 5,811 Nov-23-2019, 10:36 AM
Last Post: PillyChepper
  Tkinter Animation Evil_Patrick 3 2,619 Nov-04-2019, 06:56 PM
Last Post: Larz60+
  [Tkinter] how to remove black area around the graph in tkinter ? NEL 1 2,329 Aug-03-2019, 01:48 PM
Last Post: NEL
  Axis lim and Plotting a graph in Tkinter KEDivergente 0 1,759 May-21-2019, 08:10 PM
Last Post: KEDivergente
  Dynamic graph matplotlib quant7 1 4,188 May-17-2019, 06:24 PM
Last Post: quant7

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020