I have scoured the web for creating a live matplotlib scatter plot within a tkinter window but have found copious amounts of varying information making it confusing to decipher.As an example, I sometimes see some people use matplotlib.pyplot and I sometimes see some people use matplotlib.figure. I have no idea what the real differences between these two modules are.
I have created the example code below which I thought should simply create a matplotlib scatter plot inside the tkinter root window when I click the "Graph It" button. It does nothing when I click it though. The ultimate goal is to have a scatter plot within tkinter that updates whenever new data is read from a sensor but I'm starting simple. It should also be noted this is my first exposure to matplotlib so it may be something trivial I'm overlooking. Any help is appreciated.
#Python 3.7.9#
import tkinter as tk
from matplotlib.figure import Figure
from matplotlib.backends.backend_tkagg import (FigureCanvasTkAgg,NavigationToolbar2Tk)
import numpy as np
figure = Figure(figsize = (5,5), dpi = 100)
ax = figure.add_subplot(111)
def plot():
x = np.random.rand(1,10)
y = np.random.rand(1,10)
ax.scatter(x,y)
root = tk.Tk()
canvas = FigureCanvasTkAgg(figure, root)
canvas.draw()
canvas.get_tk_widget().pack(pady = 10)
toolbar = NavigationToolbar2Tk(canvas,root)
toolbar.update()
canvas.get_tk_widget().pack()
button = tk.Button(root, text = "Graph It", command = plot)
button.pack()
root.mainloop()