pyplot 中的等宽绘图大小,同时保持纵横比相等 [英] Equal width plot sizes in pyplot, while keeping aspect ratio equal

查看:35
本文介绍了pyplot 中的等宽绘图大小,同时保持纵横比相等的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望两个图具有相同的宽度,但是生成的代码会缩小 imshow 图.

I want to have two plots be the same width, however the resulting code shrinks the imshow plot.

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的图看起来过长(本质上,我需要适当地拉伸宽度和长度,以便长宽比仍然相等).

I would also like to keep imshow's plot from looking stretched (essentially, I need the width and length stretched appropriately so the aspect ratio's are still equal).

推荐答案

一些选项:

使用`aspect ='auto'在imshow图上

Use `aspect="auto" on the imshow plot

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

调整图形的页边距或图形大小,以使下轴具有与imshow图相同的大小,例如

Adjust the figure margings or the figure size, such that the lower axes will have the same size as the imshow plot, e.g.

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

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

You can use make_axes_locatable functionality from mpl_toolkits.axes_grid1 to divide the image axes to make space for the other axes.

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

这篇关于pyplot 中的等宽绘图大小,同时保持纵横比相等的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆