matplotlib imshow固定长宽比和垂直色条与主轴高度匹配 [英] matplotlib imshow fixed aspect and vertical colorbar matching master axis height

查看:54
本文介绍了matplotlib imshow固定长宽比和垂直色条与主轴高度匹配的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要用温度图"值绘制网格,目前我正在使用带有颜色图的 imshow.这在

有趣的是,当颜色图是水平的时,它会正确缩放:

  cax = Divider.append_axes("bottom",size ="5%",pad = 0.05)plt.colorbar(im,cax = cax,orientation =水平")

解决方案

这里发生的事情是您将 0.5 的方面应用于 imshow 图像.这会将图片的垂直范围除以2,而颜色栏则保留原始范围.我看到2种解决方案

您可以使用以下方法手动设置颜色条的大小:

cax = fig.add_axes([0.85, 0.3, 0.04, 0.4])

...或者您可以对 cax 应用一个方面,以保持其 y 维度与图像一致.在您将大小设置为5%的情况下,将Aspect = 1设置将使您的图像具有原始垂直范围的1/20.要获得1/2的图像,将纵横比设置为20 * 0.5 =10.您可以为纵横比创建一个变量,如果您想尝试更改图形的纵横比,则会出现彩条.

 将matplotlib.pyplot导入为plt从 mpl_toolkits.axes_grid1 导入 make_axes_locatable将numpy导入为np无花果= plt.figure()ax = plt.gca()im = ax.imshow(np.arange(100).reshape((10,10)),Aspect = 0.5)#在斧头的右侧创建一个轴.cax的宽度将为5%# of ax 以及 cax 和 ax 之间的填充将固定为 0.05 英寸.除法器= make_axes_locatable(ax)cax =divider.append_axes("right", size="5%", pad=0.05, aspect=10)#cax = fig.add_axes([0.85, 0.3, 0.04, 0.4])plt.colorbar(im, cax=cax)plt.show()

I need to plot a mesh grid with "temperature map" values, currently I'm using imshow, with a colormap. This is described in the Matplotlib overview, so I modified the example to force custom aspect of the figure:

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

plt.figure()
ax = plt.gca()
im = ax.imshow(np.arange(100).reshape((10,10)), aspect=0.5)

# create an axes on the right side of ax. The width of cax will be 5%
# of ax and the padding between cax and ax will be fixed at 0.05 inch.
divider = make_axes_locatable(ax)
cax = divider.append_axes("right", size="5%", pad=0.05)

plt.colorbar(im, cax=cax)

plt.savefig("test.png")

But the result is not what I want, the colorbar is higher than the master axis:

Interestingly, when the colormap is horizontal, it is scaled properly:

cax = divider.append_axes("bottom", size="5%", pad=0.05)
plt.colorbar(im, cax=cax, orientation="horizontal")

解决方案

What happens here is that you applied an aspect of 0.5 to the imshow image. This divides the vertical extend of your image by 2, while the colorbar keeps the original extent. I see 2 solutions

you can manually set the size of the colorbar using:

cax = fig.add_axes([0.85, 0.3, 0.04, 0.4])

...or you can apply an aspect to cax to keep its y-dimension consequent with the image. In your case as you set size to 5%, setting aspect=1 would give you an image with 1/20 of the original vertical-extent. To obtain 1/2 as for the image set aspect to 20*0.5= 10. You could create a variable for aspect, if you want to experiment with changing the aspect on the figure, the colorbar will follow.

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

fig = plt.figure()
ax = plt.gca()
im = ax.imshow(np.arange(100).reshape((10,10)), aspect=0.5)

# create an axes on the right side of ax. The width of cax will be 5%
# of ax and the padding between cax and ax will be fixed at 0.05 inch.
divider = make_axes_locatable(ax)
cax = divider.append_axes("right", size="5%", pad=0.05, aspect=10)
#cax = fig.add_axes([0.85, 0.3, 0.04, 0.4])
plt.colorbar(im, cax=cax)

plt.show()

这篇关于matplotlib imshow固定长宽比和垂直色条与主轴高度匹配的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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