python - matplotlib 改变子图的大小

标签 python matplotlib axes subplot

我尝试用不同的子图制作一个图形。在示例中,左侧面板是 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()

enter image description here

最佳答案

this answer 中得到启发,您可以使用 AxisDivider 调整 colorbar 的布局.

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()

enter image description here

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

关于python - matplotlib 改变子图的大小,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30576628/

相关文章:

python - 某些边有边框

python - 3D 轴上填充等高线图中的伪影

python - matplotlib 中的动画 matshow 函数

r - ggplot : scale second axis with error bars

python - 尝试在 ubuntu 上安装 matplotlib 时出现编码错误

Python3 查找字符串的 crc32

r - 如何将 sec_axis() 用于 ggplot2 R 中的离散数据?

javascript - Highcharts:使 X 轴和 Y 轴相交于 0

python - Django/djcelery 1.8.2 AppRegistryNotReady : Translation infrastructure cannot be initialized

matplotlib - 数字化颜色图