在光谱图的x轴上获得更多的时间点

2024-09-27 21:34:05 发布

您现在位置:Python中文网/ 问答频道 /正文

所以我代表了一个相对较长的视频(15分钟)的光谱图。在时间轴上,我得到了每3分20秒的数据点。 这是我的光谱图: enter image description here

我想有数据点在x轴上的每一秒。我试着从代码中修改函数timeTicks,但它不起作用。作为另一种选择,我看到我可以放大我的光谱图,但我似乎无法让它工作。你知道吗

代码如下:

import matplotlib.pyplot as plt
import scipy.io.wavfile as wavfile

cmap = plt.get_cmap('plasma') # this may fail on older versions of matplotlib
vmin = -40  # hide anything below -40 dB
cmap.set_under(color='k', alpha=None)

rate, frames = wavfile.read("audio_test.wav")
fig, ax = plt.subplots()
pxx, freq, t, cax = ax.specgram(frames[:, 0], # first channel
                                Fs=rate,      # to get frequency axis in Hz
                                cmap=cmap, vmin=vmin)
cbar = fig.colorbar(cax)
cbar.set_label('Intensity dB')
ax.axis("tight")

# Prettify
import matplotlib
import datetime

ax.set_xlabel('time h:mm:ss')
ax.set_ylabel('frequency kHz')

scale = 1e3                     # KHz
ticks = matplotlib.ticker.FuncFormatter(lambda x, pos: '{0:g}'.format(x/scale))
ax.yaxis.set_major_formatter(ticks)

def timeTicks(x, pos):
    d = datetime.timedelta(seconds=x)
    return str(d)
formatter = matplotlib.ticker.FuncFormatter(timeTicks)
ax.xaxis.set_major_formatter(formatter)
ax.xaxis.set_major_locator(ticker.IndexLocator(base=180, offset=60))
plt.show()

编辑: 目前的频谱图: enter image description here


Tags: 数据代码importmatplotlibformatterplt光谱ax

热门问题