2012-04-30 20 views
116

Necesito agregar dos subtramas a una figura. Una subtrama necesita ser aproximadamente tres veces más ancha que la segunda (misma altura). Lo logré usando GridSpec y el argumento colspan, pero me gustaría hacer esto usando figure para poder guardarlo en PDF. Puedo ajustar la primera figura usando el argumento figsize en el constructor, pero ¿cómo puedo cambiar el tamaño del segundo gráfico?Subtramas de tamaño diferente de Matplotlib

+2

Gridspec funciona con una figura normal. – tillsten

Respuesta

165

Puede utilizar gridspec y figure:

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(1, 2, width_ratios=[3, 1]) 
ax0 = plt.subplot(gs[0]) 
ax0.plot(x, y) 
ax1 = plt.subplot(gs[1]) 
ax1.plot(y, x) 

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

resulting plot

20

que utilizan pyplot 's axes objeto de ajustar manualmente los tamaños sin necesidad de utilizar GridSpec:

import matplotlib.pyplot as plt 
import numpy as np 
x = np.arange(0, 10, 0.2) 
y = np.sin(x) 

# definitions for the axes 
left, width = 0.07, 0.65 
bottom, height = 0.1, .8 
bottom_h = left_h = left+width+0.02 

rect_cones = [left, bottom, width, height] 
rect_box = [left_h, bottom, 0.17, height] 

fig = plt.figure() 

cones = plt.axes(rect_cones) 
box = plt.axes(rect_box) 

cones.plot(x, y) 

box.plot(y, x) 

plt.show() 
+2

¡Útil para aquellos de nosotros que todavía estamos en matplotlib 0.99 sin gridspec! – timday

+1

Útil para aquellos donde gridspec es inadecuado – dreab

21

Probablemente la forma más sencilla está usando subplot2grid, descrito i n Customizing Location of Subplot Using GridSpec.

ax = plt.subplot2grid((2, 2), (0, 0)) 

es igual a

import matplotlib.gridspec as gridspec 
gs = gridspec.GridSpec(2, 2) 
ax = plt.subplot(gs[0, 0]) 

así el ejemplo de BMU se convierte en:

import numpy as np 
import matplotlib.pyplot as plt 

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

# plot it 
fig = plt.figure(figsize=(8, 6)) 
ax0 = plt.subplot2grid((1, 3), (0, 0), colspan=2) 
ax0.plot(x, y) 
ax1 = plt.subplot2grid((1, 3), (0, 2)) 
ax1.plot(y, x) 

plt.tight_layout() 
plt.savefig('grid_figure.pdf') 
154

Otra forma es utilizar la función de subplots y pasar la relación de anchura con gridspec_kw:

import numpy as np 
import matplotlib.pyplot as plt 

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

# plot it 
f, (a0, a1) = plt.subplots(1,2, gridspec_kw = {'width_ratios':[3, 1]}) 
a0.plot(x,y) 
a1.plot(y,x) 

f.tight_layout() 
f.savefig('grid_figure.pdf') 
+20

En realidad, me gusta más esta opción, me alegro de haber desplazado hasta el fondo :) – astrojuanlu

+1

Gracias por esto; la forma de hacer 'plt.subplots' es mucho más limpia. –

+2

Me gustan las subtramas mejor que gridspec ya que ya no tiene que ocuparse de las configuraciones de la lista para el eje (con gridspec todavía necesita hacer el eje y las representaciones una a una). Entonces las subtramas son más limpias y más rápidas de usar –

Cuestiones relacionadas