pyplot中的等宽度绘图大小,同时保持纵横比相等

时间:2017-04-12 15:03:01

标签: python-2.7 matplotlib

我希望两个图的宽度相同,但结果代码缩小了imshow图。

xx = np.linspace(0.0,255.5,512)
yy = np.linspace(0.0,255.5,512)
Func = np.random.rand(len(xx),len(yy))

f, axarr = plt.subplots(2,1)
f.tight_layout()

im = axarr[0].imshow(Func, cmap = 'jet', interpolation = 'lanczos',origin = 'lower')
pos = axarr[0].get_position()
colorbarpos = [pos.x0+1.05*pos.width,pos.y0,0.02,pos.height]
cbar_ax = f.add_axes(colorbarpos)
cbar = f.colorbar(im,cax=cbar_ax)

axarr[1].plot(xx,Func[:,255],yy,Func[255,:])

plt.show()
plt.close('all')

编辑:我还想保持imshow的情节不被拉伸(基本上,我需要适当拉伸宽度和长度,以使宽高比仍然相等)。

1 个答案:

答案 0 :(得分:3)

一些选项:

一种。 `方面="自动"

使用`aspect =" auto"在imshow情节

    plt.imshow(...,  aspect="auto")

enter image description here

B中。调整数字边距

调整图形边距或图形尺寸,使下轴与imshow图形具有相同的尺寸,例如

    plt.subplots_adjust(left=0.35, right=0.65)

enter image description here

℃。使用分隔符

您可以使用make_axes_locatable中的mpl_toolkits.axes_grid1功能划分图像轴,为其他轴腾出空间。

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

xx = np.linspace(0.0,255.5,512)
yy = np.linspace(0.0,255.5,512)
Func = np.random.rand(len(xx),len(yy))

fig, ax = plt.subplots(figsize=(4,5))

im = ax.imshow(Func, cmap = 'jet', interpolation = 'lanczos',origin = 'lower')

divider = make_axes_locatable(ax)
ax2 = divider.append_axes("bottom", size=0.8, pad=0.3)
cax = divider.append_axes("right", size=0.08, pad=0.1)

ax2.plot(xx,Func[:,255],yy,Func[255,:])
cbar = fig.colorbar(im,cax=cax)

plt.show()

enter image description here

相关问题