[英]How to create subplot using matplotlib in Python
I write this code and I have error in my subplot.我写了这段代码,我的子图中有错误。 I don't now what is wrong in my code.
我现在不知道我的代码有什么问题。 Can you help me ?
你能帮助我吗 ?
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()
The error:错误:
raise ValueError('Illegal argument(s) to subplot: %s' % (args,))
ValueError: Illegal argument(s) to subplot: (2, 1)
As the error clearly states, you passed an illegal argument to pyplot.subplot()
.正如错误明确指出的那样,您向
pyplot.subplot()
传递了一个非法参数。 If you look at the documentation for that function , you'll see that it takes 3 arguments (which can be condensed in one): ax = plt.subplot(2, 1, 1)
or ax = plt.subplot(211)
.如果您查看该函数的文档,您会发现它需要 3 个参数(可以浓缩为一个):
ax = plt.subplot(2, 1, 1)
或ax = plt.subplot(211)
。
However, the function that you are looking for is plt.subplots()
(note the s
at the end), which generates both a figure and an array of subplots :但是,您要查找的函数是
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)
It seems that this bug is in the documentation see https://matplotlib.org/3.2.1/api/_as_gen/matplotlib.pyplot.subplots.html .似乎此错误在文档中,请参阅https://matplotlib.org/3.2.1/api/_as_gen/matplotlib.pyplot.subplots.html 。 They forgot the "s" in the third example.
他们忘记了第三个例子中的“s”。 The first two examples are correct however.
然而,前两个例子是正确的。
eg例如
# 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.