我写了这段代码,但我的子图中有错误。我现在不知道我的代码有什么问题。你能帮助我吗 ?
import pywt
import scipy.io.wavfile as wavfile
import matplotlib.pyplot as plt
rate,signal = wavfile.read('a0025.wav')
time = [x /rate for x in range(0,len(signal))]
tree = pywt.wavedec(data=signal[:1000], wavelet='db2', level=4, mode='symmetric')
print(len(tree))
newTree = [tree[0]*0, tree[1]*0, tree[2]*0, tree[3]*0, tree[4]]
recSignal = pywt.waverec(newTree,'db2')
fig, ax = plt.subplot(2, 1)
ax[0].plot(time[:1000], signal[:1000])
ax[0].set_xlabel('Czas [s]')
ax[0].set_ylabel('Amplituda')
ax[1].plot(time[:1000], recSignal[:1000])
ax[1].set_xlabel('Czas [s]')
ax[1].set_ylabel('Amplituda')
plt.show()
错误:
raise ValueError('Illegal argument(s) to subplot: %s' % (args,))
ValueError: Illegal argument(s) to subplot: (2, 1)
最佳答案
正如错误明确指出的那样,您向pyplot.subplot()
传递了非法参数。如果查看documentation for that function,您会看到它带有3个参数(可以压缩为一个):ax = plt.subplot(2, 1, 1)
或ax = plt.subplot(211)
。
但是,您要查找的功能是plt.subplots()
(请注意最后的s
)which generates both a figure and an array of subplots:
f, (ax1, ax2) = plt.subplots(1, 2, sharey=True)
ax1.plot(x, y)
ax1.set_title('Sharing Y axis')
ax2.scatter(x, y)
关于python - 如何在Python中使用Matplotlib创建子图,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/54375286/