Python Forum
Embed Matplotlib Graph to Tkinter?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Embed Matplotlib Graph to Tkinter?
#1
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
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Interactive plots that can be embed in web page mouse9095 1 609 Jun-12-2023, 04:51 PM
Last Post: deanhystad
  embed python script in C programm gucio321 0 610 Feb-11-2023, 10:47 AM
Last Post: gucio321
  best way to embed passwords into scripts mikey6785 3 2,287 Aug-31-2022, 08:22 AM
Last Post: DeaD_EyE
  How to conditionally specify markers in matplotlib graph Mark17 3 3,237 Mar-03-2022, 07:42 PM
Last Post: Mark17
  blank graph with matplotlib from a csv file / data type issue arsentievalex 0 1,960 Apr-06-2021, 10:08 AM
Last Post: arsentievalex
  Matplotlib: How do I convert Dates from Excel to use in Matplotlib JaneTan 1 3,257 Mar-11-2021, 10:52 AM
Last Post: buran
  Python Matplotlib: How do I plot lines with different end point in the same graph? JaneTan 0 1,590 Feb-28-2021, 11:56 AM
Last Post: JaneTan
  Embed Python blender code flaviu2 0 1,503 Nov-16-2020, 06:33 PM
Last Post: flaviu2
  Change the scale on a Matplotlib Interactive Graph khatharsis 0 2,860 Oct-13-2019, 06:14 AM
Last Post: khatharsis
  How to embed mp3 file with Pyinstaller panoss 2 5,947 Apr-01-2019, 01:13 PM
Last Post: yleongtyl

Forum Jump:

User Panel Messages

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