Possible bug in <slider>.reset() ?

[This post might appear twice, if so, I apologize. The first version is
flagged that it has not been accepted yet]

I am controlling a simple animation with a slider, successfully. A mouse
drag updates the parameter correctly but the slider-bar position is not
updated.

If "samp" is a slider, the code
"samp.reset(amp)" generates
"TypeError: reset() takes exactly 1 argument (2 given)".

Here is more detail of the code:

···

=====
samp = Slider(axamp, 'Amp', 0.1, 10.0, valinit=amp)

def update(val):
    global amp
    amp = samp.val
# samp.reset() # if this is included, a mouse-drag does not change
value of amp. No error.
# samp.reset(amp) # if this is included, a mouse-drag changes value of
amp, but the
                     # slider-bar-position is not updated. The animation
continues to run in both cases.
                     # AND GET ERROR:
***********
File "SAS_asl.py", line 28, in update
    samp.reset(amp) #

TypeError: reset() takes exactly 1 argument (2 given)
***********

# If there is no call to reset in “update” and the following function is
included:

def reset(event):
    samp.reset() # amp is changed, slider-bar-position is not updated
and NO ERROR IS GENERATED

samp.on_changed(update) # always present!

This appears to be a bug. If not, what is wrong with the code?

--
View this message in context: http://matplotlib.1069221.n5.nabble.com/Possible-bug-in-slider-reset-tp44638.html
Sent from the matplotlib - users mailing list archive at Nabble.com.

the reset() method is meant to return the slider back to its initialized value, that’s it.

I suspect what you are having difficulties with has to do with your use of a global variable “amp”. It is going to be completely unlikely that updates to the slider’s “val” attribute would actually be updating the global variable rather than simply replacing it with a new variable. What are you trying to do?

Ben Root

···

On Wed, Dec 17, 2014 at 2:01 PM, peterfR <peter@…4612…> wrote:

[This post might appear twice, if so, I apologize. The first version is

flagged that it has not been accepted yet]

I am controlling a simple animation with a slider, successfully. A mouse

drag updates the parameter correctly but the slider-bar position is not

updated.

If “samp” is a slider, the code

“samp.reset(amp)” generates

“TypeError: reset() takes exactly 1 argument (2 given)”.

Here is more detail of the code:

=====

samp = Slider(axamp, ‘Amp’, 0.1, 10.0, valinit=amp)

def update(val):

global amp

amp = samp.val

samp.reset() # if this is included, a mouse-drag does not change

value of amp. No error.

samp.reset(amp) # if this is included, a mouse-drag changes value of

amp, but the

                 # slider-bar-position is not updated. The animation

continues to run in both cases.

                 # AND GET ERROR:

File “SAS_asl.py”, line 28, in update

samp.reset(amp)     #

TypeError: reset() takes exactly 1 argument (2 given)


If there is no call to reset in “update” and the following function is

included:

def reset(event):

samp.reset()     # amp is changed, slider-bar-position is not updated

and NO ERROR IS GENERATED

samp.on_changed(update) # always present!

This appears to be a bug. If not, what is wrong with the code?

View this message in context: http://matplotlib.1069221.n5.nabble.com/Possible-bug-in-slider-reset-tp44638.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

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.

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 <peter@…4612…> 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, 2np.pifreq, 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

Looking at the code again, you actually don’t need to go the approach of passing an argument. The global “amp” should work. You just need to not be futzing around with it. Get rid of the three commented out lines. They do you no good.

Ben Root

···

On Wed, Dec 17, 2014 at 4:06 PM, Benjamin Root <ben.root@…1304…> wrote:

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 <peter@…4612…> 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, 2np.pifreq, 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