Estoy tratando de hacer una ttwig con 7 subplots. En este momento estoy trazando dos columnas, una con cuatro plots y la otra con tres, es decir, así:
Estoy construyendo esta ttwig de la siguiente manera:
#! /usr/bin/env python import numpy as plotting import matplotlib from pylab import * x = np.random.rand(20) y = np.random.rand(20) fig = figure(figsize=(6.5,12)) subplots_adjust(wspace=0.2,hspace=0.2) iplot = 420 for i in range(7): iplot += 1 ax = fig.add_subplot(iplot) ax.plot(x,y,'ko') ax.set_xlabel("x") ax.set_ylabel("y") savefig("subplots_example.png",bbox_inches='tight')
Sin embargo, para la publicación, creo que esto se ve un poco feo, lo que me gustaría hacer es mover la última ttwig secundaria al centro entre las dos columnas. Entonces, ¿cuál es la mejor manera de ajustar la posición de la última ttwig secundaria para que se centre? Es decir, tener las primeras 6 subplots en una cuadrícula de 3X2 y la última subplot debajo centrada entre las dos columnas. Si es posible, me gustaría poder mantener el bucle for
para que pueda usar simplemente:
if i == 6: # do something to reposition/centre this plot
Gracias,
Alex
Si desea mantener el bucle for, puede organizar sus plots con subplot2grid
, que permite un parámetro colspan
:
#! /usr/bin/env python import numpy as plotting import matplotlib from pylab import * x = np.random.rand(20) y = np.random.rand(20) fig = figure(figsize=(6.5,12)) subplots_adjust(wspace=0.2,hspace=0.2) iplot = 420 for i in range(7): iplot += 1 if i == 6: ax = subplot2grid((4,8), (i/2, 2), colspan=4) else: # You can be fancy and use subplot2grid for each plot, which dosen't # require keeping the iplot variable: # ax = subplot2grid((4,2), (i/2,i%2)) # Or you can keep using add_subplot, which may be simpler: ax = fig.add_subplot(iplot) ax.plot(x,y,'ko') ax.set_xlabel("x") ax.set_ylabel("y") savefig("subplots_example.png",bbox_inches='tight')
Use la especificación de cuadrícula (doc) con una cuadrícula de 4×4, y haga que cada plot abarque 2 columnas como tales:
import matplotlib.gridspec as gridspec gs = gridspec.GridSpec(4, 4) ax1 = plt.subplot(gs[0, 0:2]) ax2 = plt.subplot(gs[0,2:]) ax3 = plt.subplot(gs[1,0:2]) ax4 = plt.subplot(gs[1,2:]) ax5 = plt.subplot(gs[2,0:2]) ax6 = plt.subplot(gs[2,2:]) ax7 = plt.subplot(gs[3,1:3]) fig = gcf() gs.tight_layout(fig) ax_lst = [ax1,ax2,ax3,ax4,ax5,ax6,ax7]