python matplotlib图上x轴的起始值不匹配

问题描述 投票:-1回答:1

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

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 s到M采样结束的任何时间。我还正确地将startstop值赋予了linspace。但是该图的第一个值仍然是0.0,而不是0.2。如何解决以上代码中的错误,使其正确地从0.2而不是x轴的0.0开始?

python-2.7 matplotlib wav waveform wave
1个回答
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

© www.soinside.com 2019 - 2024. All rights reserved.