【问题标题】:Plot time of audio in x-axis instead of the audio sampling rate in tkinter GUI display of matplotlib graph在 x 轴上绘制音频的时间,而不是在 matplotlib 图形的 tkinter GUI 显示中的音频采样率
【发布时间】:2020-04-21 22:07:58
【问题描述】:

enter image description here我是Tkinter GUI的初学者,我正在学习波形音频显示以及相应的音频时间。我想在 matplotlib 图形的 x 轴上显示音频时间。 我尝试了 axis.set_xticks() 和功能,但在我的情况下不起作用。 如果有人能解决这个问题,那将会很有帮助。谢谢。

【问题讨论】:

    标签: python matplotlib tkinter


    【解决方案1】:

    我无法运行您的 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)
    

    这应该会导致看起来像这样

    【讨论】:

    • 感谢 gehbiszumeis 的建议,但在我的情况下不起作用。错误是刻度和样本长度不匹配。 RuntimeError:定位器尝试从 -66180.0 到 1389180.0 生成 24257 个刻度:超过 Locator.MAXTICKS
    • 你得到的错误意味着由于某种原因轴对象试图产生太多的标记来标记,即len(range(-66180, 1389180, 60)) + 1 = 24257 标记。这意味着您的音频文件长 24257 秒(即 6.7 小时)。是这样吗?如果是,您可以将MultipleLocator 的参数增加到例如3600 每小时都有主要的滴答声。如果不是,则您用于绘图的时间向量不是以秒为单位的。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-02-06
    • 2019-09-24
    • 1970-01-01
    • 1970-01-01
    • 2017-03-04
    • 2011-06-10
    • 1970-01-01
    相关资源
    最近更新 更多