Python Forum

Full Version: Embed Matplotlib Graph to Tkinter?
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hi all,

I am trying to animate stock prices from a csv file on a matplotlib graph and embed this onto tkinter. The issue is that I can't seem to get it to work with tkinter.

The issue I am having is that it only shows a matplotlib graph on the tkinter window without anything on it as shown on the image.

When I run a split terminal and run my other script to generate the prices and another script to animate this using just matplotlib and no tkinter it works, but whenever I try to add it to a tkinter window it doesn't.

Here is my code:

import tkinter
from matplotlib.backends.backend_tkagg import (
    FigureCanvasTkAgg, NavigationToolbar2Tk)
from matplotlib import pyplot as plt, animation
import pandas as pd

root = tkinter.Tk()
root.title("Embed Animation to Tkinter frame")

fig = plt.Figure(dpi=100)
ax = fig.add_subplot(111)
line, = ax.plot([], [])

canvas = FigureCanvasTkAgg(fig, master=root)
canvas.draw()

canvas.get_tk_widget().pack()

def animate(i):
    data = pd.read_csv('data.csv')
    x = data['x_value']
    price = data['stock_price']
    line.set_data(x, price)
    return line,

anim = animation.FuncAnimation(fig, animate, interval=1000)
tkinter.mainloop()
Any help would be much appreciated! Smile