Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Customizing Slider Widget
#1
Photo 
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]
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Float Slider - Affecting Values in Column 'Pandas' planckepoch86 0 1,398 Jan-22-2022, 02:18 PM
Last Post: planckepoch86
  Slider puzzle captcha resolver, how do this? dw0rd1337 0 3,486 Jan-04-2021, 11:55 PM
Last Post: dw0rd1337
  Keep Toggle View through Slider Steps yourboyjoe 1 1,671 Aug-10-2020, 07:32 PM
Last Post: Yoriz
  problem with the slider widget accoumar 3 2,247 Feb-17-2020, 09:13 AM
Last Post: accoumar
  Trying to move a RGB ledstrip with slider of my phone in almost "realtime" help Edris89 0 1,956 Sep-28-2018, 11:03 PM
Last Post: Edris89

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020