0

I am trying to create 9 subplots in a 3 by 3 format in python but even though plenty of code samples exist online of the exact arguments i'm inputing exist, for some reason pyCharm insists on needing either 1 or 3 arguments rather than 2:

fig, plot_order = plot.subplot(3,3)

the error message:

Traceback (most recent call last):
...
TypeError: subplot() takes 1 or 3 positional arguments but 2 were given
Scott Hunter
  • 48,888
  • 12
  • 60
  • 101
chsgs
  • 1
  • 1
  • 2
  • 3
    Python itself doesn't make plots; please identify the module(s) you are using to do this. – Scott Hunter Mar 01 '22 at 14:05
  • [The documentation](https://matplotlib.org/stable/api/_as_gen/matplotlib.pyplot.subplot.html) for matplolib's subplot method gives information about calling it with 3 and calling it with 1 but not calling it with 2 arguments. – John Coleman Mar 01 '22 at 14:25

2 Answers2

2

replace the "subplot" with "subplots" like this:

fig, plot_order = plot.subplots(3,3)
jby
  • 21
  • 3
2

According to the documentation of matplotlib.pyplot.subplot, one needs to either use a three-digit integer (like 234) or three integers (like 2, 3, 4) to decide the position of the subplot.

import matplotlib.pyplot as plt

# Create a 3*3 arrangement and draw subplots with indices 1 and 2
fig1 = plt.subplot(331)
fig2 = plt.subplot(332)
plt.show()

This gives:

enter image description here

The white space in the figure above shows that we have the blueprint to plot other subplots in the 3*3 arrangement, but we haven't explicitly told matplotlib to draw them. Indexing starts from 1 and refers to the top-left subplot in the figure.

This can be utilised to create subplots with different sizes e.g.

fig3 = plt.subplot(331)
fig4 = plt.subplot(122)
plt.show()

gives:

enter image description here

Another way to get a m * n subplots arrangement is to use the arguments nrows and ncols of matplotlib.pyplot.subplots to get desired numbers of rows and columns in the plot:

fig, ax = plt.subplots(nrows=3, ncols=1)

which gives: enter image description here

If you are just getting started with matplotlib, you might wonder why plt.subplot returns one object and plt.subplots returns two. To understand that, please check this answer and to learn more about matplotlib, please check the matplotlib documentation.

medium-dimensional
  • 1,974
  • 10
  • 19