Python Forum
How to instantly update the plot by getting values from a Scale widget?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How to instantly update the plot by getting values from a Scale widget?
#18
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()
Reply


Messages In This Thread
RE: How to instantly update the plot by getting values from a Scale widget? - by deanhystad - May-17-2022, 12:24 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Update exist plot while change upper limit or lower limit SamLiu 1 989 Feb-05-2023, 10:09 AM
Last Post: SamLiu
Exclamation Update Point Coordinates from Mouse Event in a Plot Jguillerm 2 1,302 Jan-10-2023, 07:53 AM
Last Post: Jguillerm
  [Tkinter] Update matplotlib plot correctly Particledust 0 4,662 Apr-20-2020, 04:59 PM
Last Post: Particledust
  [Tkinter] Scale at the Top Friend 5 2,855 Jul-20-2019, 05:02 PM
Last Post: Yoriz
  Update plot by <Return> bind with entry widget Zorro 1 4,156 Mar-09-2019, 12:27 PM
Last Post: Zorro
  [Tkinter] update the content of Text widget atlass218 10 16,232 Dec-15-2018, 11:51 AM
Last Post: atlass218
  [Tkinter] Update value in Entry widget dannyH 7 27,687 Apr-02-2017, 10:12 AM
Last Post: dannyH
  [Tkinter] Resetting scale value back to 0 after the slider loses focus lwhistler 3 6,988 Mar-07-2017, 10:01 PM
Last Post: Larz60+
  Bokeh - dynamic update widget.Select values sixteenornumber 0 10,289 Dec-28-2016, 02:15 PM
Last Post: sixteenornumber

Forum Jump:

User Panel Messages

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