Bottom Page

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

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


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)

def create_values():
    return x_values, y_values

x_values = np.arange(-20,20,0.1)

for i in range(0, len(x_values)):

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


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))
Jemeronimo likes this post

Top Page

Possibly Related Threads...
Thread Author Replies Views Last Post
  [Tkinter] Is there a sample for making a gui like Explorer(win) by Tkinter? JoshuaWeiss 0 146 Mar-05-2020, 02:53 AM
Last Post: JoshuaWeiss
  Tkinter & matplotlib PillyChepper 9 610 Nov-23-2019, 10:36 AM
Last Post: PillyChepper
  Help on drawing a shape and slider mnh001 12 921 Aug-22-2019, 11:21 PM
Last Post: mnh001
  TKinter Slider widget Jemeronimo 1 1,531 Jan-29-2019, 05:32 PM
Last Post: Larz60+
  how to install numpy and matplotlib to Tkinter elwolv 4 1,749 Dec-02-2017, 06:17 PM
Last Post: Barrowman
  [Tkinter] Resetting scale value back to 0 after the slider loses focus lwhistler 3 2,942 Mar-07-2017, 10:01 PM
Last Post: Larz60+

Forum Jump:

Users browsing this thread: 1 Guest(s)