Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

matplotlib add_subplot odd number of plots

I know that add_subplot() makes a squared grid of plots, I'm doing that with a 4x4 grid but I need one more. How can I do the same but with an odd number of plots and make it look like this? odd grid

like image 273
Javier Avatar asked Dec 07 '16 18:12

Javier


People also ask

How do you plot 3 subplots in Python?

To create multiple plots use matplotlib. pyplot. subplots method which returns the figure along with Axes object or array of Axes object. nrows, ncols attributes of subplots() method determine the number of rows and columns of the subplot grid.

What is Matplotlib GridSpec?

GridSpec. Specifies the geometry of the grid that a subplot will be placed. The number of rows and number of columns of the grid need to be set. Optionally, the subplot layout parameters (e.g., left, right, etc.)

What is fig and ax in PLT?

fig : The matplotlib. pyplot. figure object to be used as a container for all the subplots. ax : A single object of the axes. Axes object if there is only one plot, or an array of axes.


1 Answers

You need to use the gridspec submodule:

fig = pyplot.figure(figsize=(6, 4))
gs = gridspec.GridSpec(nrows=6, ncols=2)

ax11 = fig.add_subplot(gs[:2, 0])
ax21 = fig.add_subplot(gs[2:4, 0])
ax31 = fig.add_subplot(gs[4:, 0])
ax12 = fig.add_subplot(gs[:3, 1])
ax22 = fig.add_subplot(gs[3:, 1])
fig.tight_layout()

enter image description here

like image 128
Paul H Avatar answered Sep 29 '22 08:09

Paul H