I'm learning tkinter and have gone through tutorials on how to embed matplotlib into tkinter but I have had no luck. I typically just end up with a blank canvas and my script does nothing.
The script pulls from another script that pulls data from a nitrate sensor (import getSUNA).
Below is my working animation `
import datetime as dt
import matplotlib.pyplot as plt
from matplotlib.animation import FuncAnimation
import getSUNA
import time
import pandas as pd
figure, ax = plt.subplots(figsize = (12,9))
ax.set_xlim(0, 10)
ax.set_ylim(0, 10)
xs = []
ys = []
def animate(i,xs,ys):
# data from suna
print('getting data from suna')
df = pd.DataFrame(columns=['Date', 'NO3uM'])
df=getSUNA.measure(df)
print(df)
# Add x and y to lists
xs.append(df.Date[0])
ys.append(float(df.NO3uM[0]))
xs = xs[-20:]
ys = ys[-20:]
print(xs,ys)
# Limit x and y lists to 20 items
# Draw x and y lists
plt.cla()
plt.plot(xs, ys)
# Format plot
plt.xticks(rotation=45, ha='right')
plt.subplots_adjust(bottom=0.30)
plt.title('nitrate animation')
plt.ylabel('nitrate uM')
#time.sleep(60)
ani = FuncAnimation(plt.gcf(), animate, fargs = (xs,ys), interval=60000)
plt.tight_layout()
plt.show()
`
Here is my attempt at embedding in tkinter. I get a canvas plot with no data and then and error occurs starting the kernel.
I've tried to embed this in a bunch of different way and nothing seams to work all with different problems. I feel like I'm missing something silly.
import tkinter as tk
from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg
import datetime as dt
import matplotlib.pyplot as plt
from matplotlib.animation import FuncAnimation
import getSUNA
import time
import pandas as pd
from tkinter import Frame,Label,Entry,Button
figure, ax = plt.subplots(figsize = (12,9))
ax.set_xlim(0, 10)
ax.set_ylim(0, 10)
xs = []
ys = []
# This function is called periodically from FuncAnimation
def animate(i,xs,ys):
# data from suna
print('getting data from suna')
df = pd.DataFrame(columns=['Date', 'NO3uM'])
df=getSUNA.measure(df)
print(df)
# Add x and y to lists
xs.append(df.Date[0])
ys.append(float(df.NO3uM[0]))
xs = xs[-20:]
ys = ys[-20:]
print(xs,ys)
# Limit x and y lists to 20 items
# Draw x and y lists
plt.cla()
plt.plot(xs, ys)
# Format plot
plt.xticks(rotation=45, ha='right')
plt.subplots_adjust(bottom=0.30)
plt.title('nitrate animation')
plt.ylabel('nitrate uM')
#time.sleep(60)
root = tk.Tk()
label = tk.Label(root,text="nitrate uM").grid(column=0, row=0)
canvas = FigureCanvasTkAgg(figure, master=root)
canvas.get_tk_widget().grid(column=0,row=1)
# Set up plot to call animate() function periodically
ani = FuncAnimation(plt.gcf(), animate, fargs = (xs,ys), interval=60000)
tk.mainloop()