Python Forum
Making a Matplotlib in Tkinter using a slider
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Making a Matplotlib in Tkinter using a slider
#1
Hello,

I'm trying to get 2 plots that react on a Slider using Tkinter. Mostly I want it to be a function of:

y=(Slider Value) * sin(x)

And on the right another function

y= x * sin(Slider Value)

Below is what I have so far, sorry if it is not neatly programmed. If I slide my Slider I would like it to overwrite the
previous graph.

Hopefully someone can help me!

import numpy as np
import math as math
import matplotlib
import tkinter as Tk
import matplotlib.pyplot as plt
from matplotlib.widgets import Slider
from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg

matplotlib.use('TkAgg')

root = Tk.Tk()
root.wm_title("Embedding in TK")
fig = plt.Figure()
canvas = FigureCanvasTkAgg(fig, root)
canvas.get_tk_widget().pack(side=Tk.TOP, fill=Tk.BOTH, expand=1)

ax1 = fig.add_subplot(121)
ax2 = fig.add_subplot(122)
fig.subplots_adjust(bottom=0.25)


def create_values():
    return x_values, y_values


x_values = np.arange(-20,20,0.1)
y_values=[]

for i in range(0, len(x_values)):
    y_values.append(math.sin(x_values[i]))

ax1.axis([(-math.pi-1), (math.pi+1), -10, 10])
ax1.plot(x_values, y_values)

ax1_value = fig.add_axes([0.12, 0.1, 0.78, 0.03])
s_time = Slider(ax1_value, 'Value', 0, 30, valinit=0)


def update(val):

    pos = s_time.val
    ax1.plot(x_values+pos,y_values)
    fig.canvas.draw_idle()


s_time.on_changed(update)

Tk.mainloop()
Reply
#2
You can try something like this
(l,) = ax1.plot(x_values, y_values)
(r,) = ax2.plot(x_values, y_values)

def update(val):
 
    pos = s_time.val
    l.set_ydata(pos * np.sin(l.get_xdata()))
    x = r.get_xdata()
    r.set_ydata(x * np.sin(pos * x))
    fig.canvas.draw_idle()
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  [Tkinter] Annotating live tkinter graph with matplotlib annotations joeypyro 1 2,219 Nov-10-2024, 04:42 AM
Last Post: joeypyro
  Interaction between Matplotlib window, Python prompt and TKinter window NorbertMoussy 3 2,793 Mar-17-2024, 09:37 AM
Last Post: deanhystad
  [Tkinter] Tkinter Matplotlib Animation Graph not rendering dimidgen 3 4,938 Mar-12-2024, 02:09 PM
Last Post: deanhystad
  neeed to activate a custom tk slider janeik 1 1,864 Oct-09-2023, 09:29 AM
Last Post: deanhystad
  How to use rangesliders feature from matplotlib in tkinter? pymn 2 4,386 Feb-28-2022, 05:06 PM
Last Post: pymn
  [Tkinter] Making entry global in tkinter with multiprocessing luckyingermany 2 3,334 Jan-21-2022, 03:46 PM
Last Post: deanhystad
  tkinter slider question Nick_tkinter 1 4,636 Feb-22-2021, 01:31 PM
Last Post: deanhystad
  Tkinter Matplotlib Nepo 1 3,522 Sep-27-2020, 10:20 AM
Last Post: Gribouillis
  [Tkinter] Is there a sample for making a gui like Explorer(win) by Tkinter? JoshuaWeiss 0 2,184 Mar-05-2020, 02:53 AM
Last Post: JoshuaWeiss
  Tkinter & matplotlib PillyChepper 9 8,816 Nov-23-2019, 10:36 AM
Last Post: PillyChepper

Forum Jump:

User Panel Messages

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