Python Forum

Full Version: Customizing Slider Widget
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
I'm using a Slider widget to horizontally scroll through the heatmap of a 4-by-10000 matrix. Each window in my slider shows 100 columns of the matrix. Each sample corresponds to a time in seconds, which is the sample number divided by the sampling frequency, fs=200. Thus, sample number 10000 corresponds to 50 seconds.

I can relabel the horizontal axis tickmarks of my scrolling plot to be in seconds, but I wish to do the same for the shown slider value. Here's what I have:

import numpy as np
import matplotlib.pyplot as plt
from matplotlib.widgets import Slider

num_samples=10000
M=np.random.rand(4,num_samples)
scroll_win_size=100

fs=200 #sampling frequency, measured in samples per second.

fig, ax = plt.subplots()
plt.subplots_adjust(bottom=0.25)

plt.imshow(M,aspect='auto',cmap='jet')
plt.colorbar()

axpos = plt.axes([0.2, 0.1, 0.65, 0.03], facecolor='lightgoldenrodyellow')
spos = Slider(axpos, 'time', 0, num_samples-scroll_win_size,valinit=0,valstep=10)


def update(val):
    pos = spos.val
    if val < num_samples-scroll_win_size:
      ax.axis([pos,pos+scroll_win_size,0,3])
      ax.xaxis.set_major_locator(plt.MaxNLocator(5)) #Specify 5 tickmarks at a time.
      xticks = ax.get_xticks()*1/fs #Scale tickmarks to be labelled in seconds.
      ax.set_xticklabels(np.around(xticks,1))
      fig.canvas.draw_idle()

spos.on_changed(update)

plt.show() 
I want the value on the slider to be labelled in seconds, not by sample number.
[Image: iiwvu.png]