Can I make matplotlib sliders more discrete?

后端 未结 2 576
庸人自扰
庸人自扰 2020-12-05 11:41

I\'m using matplotlib sliders, similar to this demo. The sliders currently use 2 decimal places and \'feel\' quite continuous (though they have to be discrete on some level)

2条回答
  •  悲&欢浪女
    2020-12-05 12:24

    If you would rather not subclass the Slider, I picked a few lines off @Joe Kington's answer to accomplish the discretization within the callback function:

    sldr = Slider(ax,'name',0.,5.,valinit=0.,valfmt="%i")
    sldr.on_changed(partial(set_slider,sldr))
    

    and then:

    def set_slider(s,val):
        s.val = round(val)
        s.poly.xy[2] = s.val,1
        s.poly.xy[3] = s.val,0
        s.valtext.set_text(s.valfmt % s.val)
    

提交回复
热议问题