2012-09-11 31 views
5

Sto provando a creare una trama con 7 sottotrame. Al momento sto tramando due colonne, una con quattro lotti e l'altra con tre, vale a dire in questo modo: enter image description hereMatstotlib: riposizionamento di una sottotrama in una griglia di sottotrame

sto costruendo questa trama nel modo folowing:

#! /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') 

Tuttavia, per la pubblicazione I pensa che questo sia un po 'brutto - quello che mi piacerebbe fare è spostare l'ultima sottotrama nel centro tra le due colonne. Quindi, qual è il modo migliore per regolare la posizione dell'ultima sottotrama in modo che sia centrata? Cioè avere le prime 6 sottotrame in una griglia 3X2 e l'ultima sottotrama al di sotto centrata tra le due colonne. Se possibile, vorrei essere in grado di mantenere l'anello for modo che possa utilizzare semplicemente:

if i == 6: 
     # do something to reposition/centre this plot  

Grazie,

Alex

+0

ha a essere una griglia 3x2? – Harpal

risposta

5

Se si desidera mantenere il ciclo for, è possibile organizzare i vostri appezzamenti con subplot2grid, che permette per un parametro 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') 

subplots in a grid with colspan

8

Uso griglia spec (doc) con una griglia 4x4, e avere ogni arco trama 2 colonne in quanto tali:

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] 
+0

Funziona perfettamente. È possibile controllare la spaziatura tra le griglie, consentendo l'asse etichettato, ecc. Utilizzando le tecniche standard matplotlib - IE 'fig = plt.figure (figsize = (10,15))' quindi dopo aver chiamato ogni sottotrama/griglia 'gs .tight_layout (fig) ' – AGS

+0

Una buona risposta! Grazie! –

Problemi correlati