1

Looking at the slider_demo.py, the author has the following code:

axcolor = 'lightgoldenrodyellow'
axfreq = plt.axes([0.25, 0.1, 0.65, 0.03], facecolor=axcolor)
axamp = plt.axes([0.25, 0.15, 0.65, 0.03], facecolor=axcolor)

sfreq = Slider(axfreq, 'Freq', 0.1, 30.0, valinit=f0, valstep=delta_f)
samp = Slider(axamp, 'Amp', 0.1, 10.0, valinit=a0)

where, in sfreq, for example, the values 0.1 and 30.0 are valmin and valmax, respectively, and the slider values are incremented by delta_f.

I would like to, instead, define an array of values

valarray = [0.1, 1, 3, 5, 15, 20, 27, 30]

where this array of values are the only values selected, displayed, or available as one moves the slider. This does not appear to be an explicit option for this widget.

It would also be helpful, but not necessary, that I am able to update this list dynamically.

Thanks in advance for your help and assistance.

Zephyr
  • 11,891
  • 53
  • 45
  • 80
calmymail
  • 127
  • 8

1 Answers1

2

You can set the major ticks of the Slider with this line:

axfreq.set_xticks(np.array([0.1, 1, 3, 5, 15, 20, 27, 30]), minor = False)

eventually, you can also set minor ticks by setting minor = True.

enter image description here

Zephyr
  • 11,891
  • 53
  • 45
  • 80
  • Thanks for the quick reply. I was not aware of this feature, but I plan to use it! However, while this shows the tick marks, the slider does not stop at each of these user defined discrete ticks, which is what I am seeking. – – calmymail Jul 09 '20 at 00:55
  • In that case, maybe you should consider to use a discrete range selection widget as a `RadioButtons`. `Slider` is thought to be used for a continuous range selection. – Zephyr Jul 09 '20 at 09:03
  • I thought about that, but unfortunately, there are too many numbers and the plot would become cluttered. – calmymail Jul 09 '20 at 18:43
  • 1
    [This does not work anymore automatically](https://stackoverflow.com/q/71186196/8881141) with matplotlib 3.5.1. – Mr. T Feb 20 '22 at 23:10