使用pyplot标记时间轴值

时间:2019-05-18 17:09:56

标签: python-2.7 matplotlib wav waveform wave

读取波形文件后,我试图绘制选定数量的样本。我编写了以下代码来实现这一目标:

import numpy as np
import matplotlib.pyplot as plt
from scipy.io.wavfile import read

(fs, x) = read('/home/sk_he/sounds/sample.wav')

M = 501
start_time = 0.2
start_sample = int(start_time * fs)
stop_sample = int(start_time * fs) + M
x1 = x[start_sample:stop_sample]
stop_time = float(stop_sample/fs)
tx1 = np.linspace(start_time, stop_time, M)
plt.plot(tx1, x1)

这给了我以下输出: Plot

尽管这很好,但我打算指出从0.2秒到M采样结束的时间。我还正确地将startstop的值赋予了linspace。但是该图的第一个值仍为0.0,而不是0.2。如何使它从0.2而不是0.0开始?

1 个答案:

答案 0 :(得分:0)

问题在于进行类型转换的位置。我已经修改了代码,它按预期显示了输出:

start_time = 0.2
start_sample = start_time * fs
stop_sample = (start_time * fs) + M
x1 = x[int(start_sample):int(stop_sample)]
stop_time = float(stop_sample/fs)
tx1 = np.linspace(start_time, stop_time, M)

下图是正确的预期输出:

CorrectPlot