Plot time of audio in x-axis instead of the audio sampling rate in tkinter GUI display of matplotlib graph Plot time of audio in x-axis instead of the audio sampling rate in tkinter GUI display of matplotlib graph tkinter tkinter

Plot time of audio in x-axis instead of the audio sampling rate in tkinter GUI display of matplotlib graph


I cannot run your MCVE, but have a look at the matplotlib.ticker class. Once you have calculated the time vector by

times = numpy.linspace(0, (self.audio.signal.shape[0]) / fs, num=(self.audio.fs))    

(which you did correctly as far as I can see), you can e.g. use MultipleLocator() to set the ticks. Note, that times has to be in units of seconds.

import time# Set major ticks every 60 secondsself.ax1.xaxis.set_major_locator(matplotlib.ticker.MultipleLocator(60))# Set minor ticks every 10 secondsself.ax1.xaxis.set_minor_locator(matplotlib.ticker.MultipleLocator(10))# Define format of xtick labels with help of `time` module as Minutes:Secondsformatter = matplotlib.ticker.FuncFormatter(lambda s, x: time.strftime('%M:%S', time.gmtime(s)))self.ax1.xaxis.set_major_formatter(formatter)

This should lead to something looking like this

enter image description here