Based on this and that examples, I am trying to add sliders to a figure dynamically (in function of the object that is plotted).
The sliders are created and added inside a for-loop, and a new update() function is created every time.
Problems: The sliders do not respond to any mouse inputs!
Any idea how to solve this?
import numpy as np from matplotlib import pyplot as plt from matplotlib.widgets import Slider class Plotter: def __init__(self): self.fig, self.ax = plt.subplots() def plot(self, obj): self.obj = obj self.l = plt.plot(obj.t,obj.series()) vars = obj.get_variables() plt.subplots_adjust(bottom=0.03*(len(vars)+2)) for i,var in enumerate(vars): self.add_slider(i*0.03, var[0], var[1], var[2]) plt.show() def add_slider(self, pos, name, min, max): ax = plt.axes([0.1, 0.02+pos, 0.8, 0.02], axisbg='lightgoldenrodyellow') slider = Slider(ax, name, min, max, valinit=getattr(self.obj, name)) def update(val): setattr(self.obj, name, val) self.l.set_ydata(self.obj.series()) self.fig.canvas.draw_idle() slider.on_changed(update) class SinFunction: def __init__(self): self.freq = 1.0 self.amp = 0.5 self.t = np.arange(0.0, 1.0, 0.001) def series(self): return self.amp*np.sin(2*np.pi*self.freq*self.t) def get_variables(self): return [ ('freq', 0.1, 10), ('amp', 0.1, 1) ] Plotter().plot(SinFunction()) 