matplotlib slider not working when called from a function

后端 未结 2 1981
小鲜肉
小鲜肉 2021-01-22 07:52

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 i         


        
2条回答
  •  南方客
    南方客 (楼主)
    2021-01-22 08:24

    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()
    

提交回复
热议问题