matplotlib 改变子图的大小

matplotlib change size of subplots

我试着用不同的子图制作一个图形。在示例中,左侧面板是 imshow 图像,它有点太小了。我怎样才能放大ax1?为了使颜色条位于 ax2ax3?

的 x 轴水平

代码和输出是:

import matplotlib.pyplot as plt
import numpy as np


fig=plt.figure(figsize=(15,5.5))
ax1=plt.subplot2grid((1,3),(0,0))
ax2=plt.subplot2grid((1,3),(0,1))
ax3=plt.subplot2grid((1,3),(0,2))


image=np.random.random_integers(1,10,size=(100,100))
cax=ax1.imshow(image,interpolation="none",aspect='equal')
cbar=fig.colorbar(cax,ax=ax1,orientation=u'horizontal')

x=np.linspace(0,1)
ax2.plot(x,x**2)
ax3.plot(x,x**3)

plt.show()

借鉴 this answer, you can adjust the layout of your colorbar with AxisDivider

import matplotlib.pyplot as plt
import numpy as np
from mpl_toolkits.axes_grid1 import make_axes_locatable

fig=plt.figure(figsize=(15,5.5))
ax1=plt.subplot2grid((1,3),(0,0))
ax2=plt.subplot2grid((1,3),(0,1))
ax3=plt.subplot2grid((1,3),(0,2))

image=np.random.random_integers(1,10,size=(100,100))
im = ax1.imshow(image,interpolation="none",aspect='equal')

divider = make_axes_locatable(ax1)
cax = divider.append_axes("bottom",size="5%",pad=0.7)
cbar=fig.colorbar(im,cax=cax,orientation=u'horizontal')

x=np.linspace(0,1)
ax2.plot(x,x**2)
ax3.plot(x,x**3)

plt.show()

pad=0.7 在我看来是正确的。您可能需要尝试使用该参数以及 figsize 高度才能完全按照您想要的方式获得它。