Changing the text displayed by slider (in real time) using a custom array

Hello there

I adapted the Update image data in a performant manner section from the Example notebook to operate on a 3D numpy array with seismic data (dimensions of x, y, time), and I am really impressed with the performance:
ezgif.com-video-to-gif-2

My question:

Is there a way for the text on the slider to print/show time values in milliseconds [0 4 8… 6000] instead of sample number [0 1 2 … 1500]? In previous realizations with ipywidgetd I just used

rslt = interactive(seismic_plot_function, w=IntSlider(min=0, max=6000, 
                   step=1, value = 100))
display(rslt)

whereby as w is changed by the slider, it is calculated to index into the times array to get a t which is then passed to a text box inside the same seismic_plot_function , something like:

ax3.text(0.65, 2, 'two way time = {0:.2f}'.format(t), ha="center", 
              va="center", size=18, bbox=bbox_props)

Thanks for your help!

The easiest way is probably to use hyperslicer: Hyperslicer Tutorial — mpl-interactions 0.17.8 documentation which will also extend to work with more dimensions and integrates with xarray.

I’ll also note that you can get the same performance as that example using mpl-interactions’ imshow function: Imshow — mpl-interactions 0.17.8 documentation. which has some slider formatting options.

If you want to do it manually you have a few options:

For ipywidgets sliders you should either use a SelectionSlider (e.g. Add string formatting to SelectionSlider · Issue #2902 · jupyter-widgets/ipywidgets · GitHub) or you can do what I do in mpl-interactions and make a custom readout using a Text widget: mpl-interactions/helpers.py at 99e1552266b8179c03583e585ad262c2670daa63 · ianhi/mpl-interactions · GitHub

For matplotlib sliders the trick is to add a callback that just modifies the displayed text: mpl-interactions/helpers.py at 99e1552266b8179c03583e585ad262c2670daa63 · ianhi/mpl-interactions · GitHub

1 Like

For hyperslicer you need this section of the docs in particular: Hyperslicer Tutorial — mpl-interactions 0.17.8 documentation

1 Like

Thanks for your quick reply @ianhi, and the variety of options.
I will be checking out all the options you suggest, but I think it would be safe to accept this as the answer (except I cannot see how to do it? It is possible on other Discourse sites)