Dec-03-2018, 01:37 PM
(This post was last modified: Dec-03-2018, 01:38 PM by Jemeronimo.)
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!
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()