[英]How to create subplot using matplotlib in Python
我寫了這段代碼,我的子圖中有錯誤。 我現在不知道我的代碼有什么問題。 你能幫助我嗎 ?
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()
傳遞了一個非法參數。 如果您查看該函數的文檔,您會發現它需要 3 個參數(可以濃縮為一個): ax = plt.subplot(2, 1, 1)
或ax = plt.subplot(211)
。
但是,您要查找的函數是plt.subplots()
(注意末尾的s
), 它生成一個圖形和一個子圖數組:
f, (ax1, ax2) = plt.subplots(1, 2, sharey=True)
ax1.plot(x, y)
ax1.set_title('Sharing Y axis')
ax2.scatter(x, y)
似乎此錯誤在文檔中,請參閱https://matplotlib.org/3.2.1/api/_as_gen/matplotlib.pyplot.subplots.html 。 他們忘記了第三個例子中的“s”。 然而,前兩個例子是正確的。
例如
# using tuple unpacking for multiple Axes
fig, (ax1, ax2) = plt.subplot(1, 2)
fig, ((ax1, ax2), (ax3, ax4)) = plt.subplot(2, 2)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.