1

I am trying to understand how Matplotlib plt.subplot actually works.

Take the following code:

import pyplot as plt    

fig = plt.figure()
plt.subplot(1,2,1)
plt.subplot(2,2,2)
plt.show()

This generates the required subplot as :

enter image description here

But now I want to add a seperate plot in the bottom right hand corner that is vacant in the picture.

For which I used the code.

plt.subplot(2,2,3)

This however does not work as intended and gives a weird plot like the following:

enter image description here

I would like to know why this happening and what I can do to rectify this. I know this is possible by using GridSpec, but is it possible through just subplot?

Also how does subplot work in cases like:

plt.subplot(4,3,3)

That is what happens when I change the grid structure on the next call? I am unable to see what the display is after I add this line.

ng.newbie
  • 2,807
  • 3
  • 23
  • 57
  • 2
    Indizes start at 1 in this case. Hence use `plt.subplot(2,2,4)` to get the subplot in the lower right corner. Refer to [what does the argument mean in fig.add_subplot(111)](https://stackoverflow.com/questions/3584805/in-matplotlib-what-does-the-argument-mean-in-fig-add-subplot111) – ImportanceOfBeingErnest Sep 06 '19 at 19:48
  • @ImportanceOfBeingErnest How can I get a bigger and smaller subplot side by side? for example for 121 and 422 you would expect a vertical subplot next to a larger vertical subplot but this is not the case. – ng.newbie Sep 07 '19 at 06:12
  • @ImportanceOfBeingErnest Why isnt there any documentation in the Matplotlib docs explaining how this works? – ng.newbie Sep 07 '19 at 06:13
  • The documentation is https://matplotlib.org/3.1.1/api/_as_gen/matplotlib.pyplot.subplot.html It explains is quite clearly, I would say. I do not know what's unexpected about 121 and 422, the result is perfectly consistent with the documentation. – ImportanceOfBeingErnest Sep 07 '19 at 11:38
  • @ImportanceOfBeingErnest I am afraid the doc does not give me any insight into how `subplot` works. Could please clarify where in the doc that you have linked I can find information about how it works ? – ng.newbie Sep 08 '19 at 08:11
  • The key sentence is "the subplot will take the *index* position on a grid with *nrows* rows and *ncols* columns. *index* starts at 1 in the upper left corner and increases to the right." (The signature is `subplot(nrows,ncols,index)`) That's really all you need to know. If the problem with 121 and 422 persists, you could show the output in the question and use it to explain how you would have it instead. – ImportanceOfBeingErnest Sep 08 '19 at 11:32

0 Answers0