2017-03-28 43 views
0

我編寫了這段代碼,並且在我的子圖中出現錯誤。我現在不在我的代碼中出現什麼問題。你可以幫我嗎 ?如何在Python中使用matplotlib創建子圖

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) 
+3

我們不想讀所有的代碼,而這些鏈接可能會最終失敗。請將相關部分粘貼到您的問題中。 –

回答

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) 
相關問題