Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

427
Vistas
Uneven subplot in python

What is the best way to create a (3,3) subplot matrix in python with the following catch:

  • first column contains 3 subplots
  • second column contains 3 subplots
  • third column contains 2 subplots

The last two subplots should have equal size. This means they will meet in the middle of the middle plot for the other two columns.

I tried to do this with gridspec but did not managed so far.

import numpy as np
import matplotlib.pyplot as plt
from matplotlib import gridspec

# generate some data
x = np.arange(0, 10, 0.2)
y = np.sin(x)

# plot it
fig = plt.figure(figsize=(8, 6))

gs = gridspec.GridSpec(3, 3)

ax0 = plt.subplot(gs[0])
ax0.plot(x, y)
ax1 = plt.subplot(gs[1])
ax1.plot(y, x)

ax3 = plt.subplot(gs[3])
ax3.plot(y, x)
ax4 = plt.subplot(gs[4])
ax4.plot(y, x)

ax6 = plt.subplot(gs[6])
ax6.plot(y, x)
ax7 = plt.subplot(gs[7])
ax7.plot(y, x)

plt.tight_layout()
plt.savefig('grid_figure.png')

plt.show()

enter image description here

over 4 years ago · Santiago Trujillo
3 Respuestas
Responde la pregunta

0

No need to use gridspec here. Just add the subplots where you want to have them.

import numpy as np
import matplotlib.pyplot as plt
from matplotlib import gridspec

# generate some data
x = np.arange(0, 10, 0.2)
y = np.sin(x)

# plot it
fig = plt.figure(figsize=(8, 6))

ax1= fig.add_subplot(3,3,1)
ax2= fig.add_subplot(3,3,2)
ax3= fig.add_subplot(3,3,4)
ax4= fig.add_subplot(3,3,5)
ax5= fig.add_subplot(3,3,7)
ax6= fig.add_subplot(3,3,8)

ax7= fig.add_subplot(2,3,3)
ax8= fig.add_subplot(2,3,6)

plt.show()

enter image description here

over 4 years ago · Santiago Trujillo Denunciar

0

I assume your problem is with the two plots in the last row. One idea could be to think of the grid as a (3,6) grid. You use two columns for each plot in the first two rows and 3 columns for the two plots in the last row.

ax1 = plt.subplot2grid((3, 6), (0, 0), colspan=2)
ax2 = plt.subplot2grid((3, 6), (0, 2), colspan=2)
ax3 = plt.subplot2grid((3, 6), (0, 4), colspan=2)
ax4 = plt.subplot2grid((3, 6), (1, 0), colspan=2)
ax5 = plt.subplot2grid((3, 6), (1, 2), colspan=2)
ax6 = plt.subplot2grid((3, 6), (1, 4), colspan=2)
ax7 = plt.subplot2grid((3, 6), (2, 0), colspan=3)
ax8 = plt.subplot2grid((3, 6), (2, 3), colspan=3)
over 4 years ago · Santiago Trujillo Denunciar

0

Provided answer as answer from the question. The following was written by OP:

Thanks @datasailor for the solution. Here is a working example:

import numpy as np
import matplotlib.pyplot as plt
from matplotlib import gridspec

# generate some data
x = np.arange(0, 10, 0.2)
y = np.sin(x)

# plot it
fig = plt.figure(figsize=(8, 6))

gs = gridspec.GridSpec(6, 3)

ax0 = plt.subplot(gs[0:2,0])
ax0.plot(x, y)
ax1 = plt.subplot(gs[2:4,0])
ax1.plot(y, x)
ax2 = plt.subplot(gs[4:6,0])
ax2.plot(y, x)

ax3 = plt.subplot(gs[0:2,1])
ax3.plot(x, y)
ax4 = plt.subplot(gs[2:4,1])
ax4.plot(y, x)
ax5 = plt.subplot(gs[4:6,1])
ax5.plot(y, x)

ax6 = plt.subplot(gs[0:3,2])
ax6.plot(x, y)
ax7 = plt.subplot(gs[3:6,2])
ax7.plot(y, x)


plt.tight_layout()
plt.savefig('grid_figure.png')

plt.show()

enter image description here

over 4 years ago · Santiago Trujillo Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda