Skip to content Skip to sidebar Skip to footer

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 a

Solution 1:

As the error clearly states, you passed an illegal argument to 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).

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:

f, (ax1, ax2) = plt.subplots(1, 2, sharey=True)
ax1.plot(x, y)
ax1.set_title('Sharing Y axis')
ax2.scatter(x, y)

Solution 2:

It seems that this bug is in the documentation see https://matplotlib.org/3.2.1/api/_as_gen/matplotlib.pyplot.subplots.html. They forgot the "s" in the third example. The first two examples are correct however. e.g.

# using tuple unpacking for multiple Axes
fig, (ax1, ax2) = plt.subplot(1, 2)
fig, ((ax1, ax2), (ax3, ax4)) = plt.subplot(2, 2)

Post a Comment for "How To Create Subplot Using Matplotlib In Python"