May-17-2022, 12:24 AM
(This post was last modified: May-17-2022, 01:22 AM by deanhystad.)
I reworked the colormesh animation I mentioned above to use a scale object in a tkinter window.
import numpy as np from matplotlib import pyplot as plt, animation import tkinter as tk plt.rcParams["figure.figsize"] = [7.00, 3.50] plt.rcParams["figure.autolayout"] = True # This is the plot that you call when first opening the plot window. fig, ax = plt.subplots() x = np.linspace(-3, 3, 91) t = np.linspace(0, 25, 30) y = np.linspace(-3, 3, 91) X3, Y3, T3 = np.meshgrid(x, y, t) sinT3 = np.sin(2 * np.pi * T3 / T3.max(axis=2)[..., np.newaxis]) G = (X3 ** 2 + Y3 ** 2) * sinT3 cax = ax.pcolormesh(x, y, G[:-1, :-1, 0], vmin=-1, vmax=1, cmap='Blues') fig.colorbar(cax) plt.show(block=False) # This is the function called when the slider is moved. def update_plot(level): cax.set_array(G[:-1, :-1, int(level)].flatten()) fig.canvas.draw() root = tk.Tk() slider = tk.Scale( root, from_=1, to=len(t)-1, orient="horizontal", length=100, command=update_plot) slider.pack() root.mainloop()And here it is using a canvas in a tkinter window.
import tkinter as tk import numpy as np from matplotlib.figure import Figure from matplotlib import cm from matplotlib.colors import ListedColormap from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg class SampleApp(tk.Tk): def __init__(self, *args, **kwargs): super().__init__(*args, **kwargs) self.layer = tk.IntVar(self, 1) self.plot() canvas = FigureCanvasTkAgg(self.figure, self) canvas.draw() canvas.get_tk_widget().pack(padx=10, pady=10) self.slider = tk.Scale(self, from_=0, to=29, orient="horizontal", length=300, command=self.update_plot) self.slider.pack(padx=10, pady=10) def plot(self): x = np.linspace(-3, 3, 91) t = np.linspace(0, 25, 30) y = np.linspace(-3, 3, 91) X3, Y3, T3 = np.meshgrid(x, y, t) sinT3 = np.sin(2 * np.pi * T3 / T3.max(axis=2)[..., np.newaxis]) self.data = (X3 ** 2 + Y3 ** 2) * sinT3 self.figure = Figure(figsize=(4, 4)) ax = self.figure.add_subplot(111) self.mesh = ax.pcolormesh(x, y, self.data[:-1, :-1, 0], vmin=-1, vmax=1, cmap='Blues') self.figure.colorbar(self.mesh) def update_plot(self, level): level = int(level) self.mesh.set_array(self.data[:-1, :-1, level].flatten()) self.figure.canvas.draw() app = SampleApp() app.mainloop()