7

I have the following code, which shows a graph with a slinding bar

    from matplotlib.widgets import Slider
    import matplotlib.pyplot as plt
    from numpy import arange, sin, pi

    t = arange(0.0, 10.0, 0.01)

    fig =plt.figure(figsize = [30,20])
    ax = plt.subplot(111)
    plt.plot(t, sin(t*10))
    plt.ylim((-2,2))
    plt.xlim((0,1))
    plt.tight_layout()

    axzoom= plt.axes([0.15, 0.05, 0.65, 0.03])
    szoom = Slider(axzoom, 'Window', 1, 2)

    def update(val):
        ax.set_xlim([val,val+1])
        fig.canvas.draw_idle()
    szoom.on_changed(update)
    plt.show()

I want to call it from a function, that is:

def myplot(): 
    (... same code as above)

The problem is, when I do that the slinding bar does not work anymore. Any idea of what I could be doing wrong?

I am trying to execute it from iPython within Spyder. Thank you for any help.

Sininho
  • 287
  • 5
  • 13

3 Answers3

9

Based on this you need to keep the sliders around globally.

Your example would only need to be slightly adjusted:

def myplot(): 
    (... same code as above)
    return szoom

keep_sliders = myplot()
Dunkelkoon
  • 398
  • 2
  • 10
  • This worked for me. Bonus points for finding the github issue! – eric Dec 15 '20 at 22:38
  • 1
    I should add it is crucial to actually assign the output of `myplot()` a value (`keep_sliders` in this case) otherwise it will not work. I know this should be obvious, as that is how you keep the sliders around globally, but I just spent 20 minutes debugging this. – eric Dec 17 '20 at 22:06
  • 1
    Btw: you do not necessarily have to store the Slider object itself globally, it is also fine to have it as member variable (or even list element) of a class which is stored globally (e.g. `visualizer = Visualizer()` with `class Visualizer: __init__(self): self.sliderax = Slider(axzoom, 'Window', 1, 2)`) – F1iX Feb 09 '21 at 11:52
1

It is better to use axzoom = fig.add_axes([0.12, 0.1, 0.78, 0.03]) instead of axzoom = plt.axes([0.15, 0.05, 0.65, 0.03]) .

from matplotlib.widgets import Slider
import matplotlib.pyplot as plt
from numpy import arange, sin

def myplot():
    fig = plt.figure(figsize=[30, 20])
    ax = plt.subplot(111)
    plt.tight_layout()
    t = arange(0.0, 10.0, 0.01)
    plt.plot(t, sin(t * 10))
    plt.ylim((-2, 2))
    plt.xlim((0, 1))

    # axzoom = plt.axes([0.15, 0.05, 0.65, 0.03])
    axzoom = fig.add_axes([0.12, 0.1, 0.78, 0.03])
    szoom = Slider(axzoom, 'Window', 1, 2)
    szoom.on_changed(lambda val: ax.set_xlim([val, val + 1]))

    fig.canvas.draw_idle()
    plt.show()

if __name__ == '__main__':
    myplot()
chengzi
  • 11
  • 2
0

Okay, here is my working version with some improvements (e.g. lambda):

from matplotlib.widgets import Slider
import matplotlib.pyplot as plt
from numpy import arange, sin, pi

def myplot():
    t = arange(0.0, 10.0, 0.01)

    fig = plt.figure(figsize=[30, 20])
    ax = plt.subplot(111)
    plt.tight_layout()
    plt.plot(t, sin(t * 10))
    plt.ylim((-2, 2))
    plt.xlim((0, 1))

    axzoom = plt.axes([0.15, 0.05, 0.65, 0.03])
    szoom = Slider(axzoom, 'Window', 1, 2)
    szoom.on_changed(lambda val: ax.set_xlim([val, val + 1]))

    fig.canvas.draw_idle()
    plt.show()

myplot()
Darius
  • 10,762
  • 2
  • 29
  • 50
  • Did not work. Maybe the problem has something to do with Spyder? – Sininho May 04 '16 at 11:16
  • Does the [official example](http://matplotlib.org/examples/widgets/slider_demo.html) work? Maybe it's an issue with your (rendering) backend. – Darius May 04 '16 at 11:57
  • 1
    Okay, then try to adapt parts and/or uncomment optional commands. I can't debug it, because I can't reproduce the issue. – Darius May 04 '16 at 12:10