【发布时间】:2020-04-21 22:07:58
【问题描述】:
enter image description here我是Tkinter GUI的初学者,我正在学习波形音频显示以及相应的音频时间。我想在 matplotlib 图形的 x 轴上显示音频时间。 我尝试了 axis.set_xticks() 和功能,但在我的情况下不起作用。 如果有人能解决这个问题,那将会很有帮助。谢谢。
【问题讨论】:
标签: python matplotlib tkinter
enter image description here我是Tkinter GUI的初学者,我正在学习波形音频显示以及相应的音频时间。我想在 matplotlib 图形的 x 轴上显示音频时间。 我尝试了 axis.set_xticks() 和功能,但在我的情况下不起作用。 如果有人能解决这个问题,那将会很有帮助。谢谢。
【问题讨论】:
标签: python matplotlib tkinter
我无法运行您的 MCVE,但请查看 matplotlib.ticker class。一旦你通过
times = numpy.linspace(0, (self.audio.signal.shape[0]) / fs, num=(self.audio.fs))
(据我所知,您做得正确),例如使用MultipleLocator() 设置刻度。请注意,times 必须以秒为单位。
import time
# Set major ticks every 60 seconds
self.ax1.xaxis.set_major_locator(matplotlib.ticker.MultipleLocator(60))
# Set minor ticks every 10 seconds
self.ax1.xaxis.set_minor_locator(matplotlib.ticker.MultipleLocator(10))
# Define format of xtick labels with help of `time` module as Minutes:Seconds
formatter = matplotlib.ticker.FuncFormatter(lambda s, x: time.strftime('%M:%S', time.gmtime(s)))
self.ax1.xaxis.set_major_formatter(formatter)
这应该会导致看起来像这样
【讨论】:
len(range(-66180, 1389180, 60)) + 1 = 24257 标记。这意味着您的音频文件长 24257 秒(即 6.7 小时)。是这样吗?如果是,您可以将MultipleLocator 的参数增加到例如3600 每小时都有主要的滴答声。如果不是,则您用于绘图的时间向量不是以秒为单位的。