First, I am not sure what you think the reset() method does. samp.reset()
does one job. It RESETS the slider. You are not seeing the bar move because
it keeps getting reset. Take it out from your code. I do not know what you
are trying to do with it.

Second, numerical values in python are not mutable. The Slider can modify
its "val" attribute to its heart's content. You have *zero* guarantees that
samp.val is still the same variable from one point to the next.
Furthermore, assigning an immutable value to a global variable does you no
good anyway, as it is the "samp.val" variable that gets modified by the
slider object, and it isn't global.

You might be seeing things that sort of looks like you want, but you really
can't be sure that it isn't the result of some strange side-effects due to
the interactive backend swallowing exceptions every time you touch that
slider.

What you want is to have the animate() function take a second argument
being the slider object (or go with the global approach, if you want).
Access the "val" attribute from the slider object for the set_ydata() call.
If you go with the passing of the slider object as an argument, you can
then pass it in a tuple to the "fargs" argument of the FuncAnimation
constructor.

I hope that clears it up for you.
Cheers!
Ben Root


On Wed, Dec 17, 2014 at 3:49 PM, peterfR <pe...@pelican.ucsd.edu> wrote:
>
> I have a simple oscillatory animation with a parameter "amp", which I
> control
> with a slider.
> The control works (when I do a mouse-drag on the slider bar), but the slide
> bar never actually changes it's position because the reset call fails.
> I don't see why the global statement should have any effect.
> I want the "amp" variable to be common to the slider and the animation.
> I'll include the whole code, it's only about 30 lines of executable code:
> ====
> """
>  A simple example of an animated plot, controlled by a slider
> """
> import numpy as np
> import matplotlib.pyplot as plt
> import matplotlib.animation as animation
> from matplotlib.widgets import Slider
>
> fig, ax = plt.subplots()
> amp = 5
> freq = 1
> t = np.arange(0.0, 2*np.pi*freq, 0.01)
> print("len(t)=", len(t))
> # INITIALIZE CURVE
> line, = ax.plot(t, amp*np.sin(t), lw=4, color='purple')
>
> # Setup slider
>
> axcolor = 'lightgoldenrodyellow'
> axamp  = plt.axes([0.25, 0.15, 0.60, 0.03], axisbg=axcolor)
>
> samp = Slider(axamp, 'Amp', 0.1, 10.0, valinit=amp)
>
> def update(val):
>     global amp
>     amp = samp.val
> #   samp.set_val(amp)
> #   samp.reset()     # if no argument given, the value of amp is not
> changed.
> #   samp.reset(amp)     # If used, amp is changed, the animation continues
> with changed amp value,
>                                    # but the slider bar does
>                                    # not move and get the error "TypeError:
> reset() takes exactly 1 argument (2 given)"
>
> samp.on_changed(update)
>
> def reset(event):
>     samp.reset()
>
> ## Do the animation
>
> def animate(i):
>     line.set_ydata(amp*np.sin(t+(i/10.0)))  # update the data
>     return line,
>
> #Init only required for blitting to give a clean slate.
> def init():
>     line.set_ydata(np.ma.array(t, mask=True))
>     return line,
>
> ani = animation.FuncAnimation(fig, animate, np.arange(1, 100000),
> init_func=init,
>     interval=20, repeat=True, repeat_delay=200, blit=True)
> plt.show()
>
>
>
>
> --
> View this message in context:
> http://matplotlib.1069221.n5.nabble.com/Possible-bug-in-slider-reset-tp44638p44640.html
> Sent from the matplotlib - users mailing list archive at Nabble.com.
>
>
> ------------------------------------------------------------------------------
> Download BIRT iHub F-Type - The Free Enterprise-Grade BIRT Server
> from Actuate! Instantly Supercharge Your Business Reports and Dashboards
> with Interactivity, Sharing, Native Excel Exports, App Integration & more
> Get technology previously reserved for billion-dollar corporations, FREE
>
> http://pubads.g.doubleclick.net/gampad/clk?id=164703151&iu=/4140/ostg.clktrk
> _______________________________________________
> Matplotlib-users mailing list
> Matplotlib-users@lists.sourceforge.net
> https://lists.sourceforge.net/lists/listinfo/matplotlib-users
>
------------------------------------------------------------------------------
Download BIRT iHub F-Type - The Free Enterprise-Grade BIRT Server
from Actuate! Instantly Supercharge Your Business Reports and Dashboards
with Interactivity, Sharing, Native Excel Exports, App Integration & more
Get technology previously reserved for billion-dollar corporations, FREE
http://pubads.g.doubleclick.net/gampad/clk?id=164703151&iu=/4140/ostg.clktrk
_______________________________________________
Matplotlib-users mailing list
Matplotlib-users@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/matplotlib-users

Reply via email to