gpt4 book ai didi

python - 如何使图像和散点图具有相同的高度?

转载 作者:行者123 更新时间:2023-11-30 23:06:58 24 4
gpt4 key购买 nike

我正在绘制两个不同的子图。一个是图像,另一个是散点图。

fig = plt.figure(figsize=(16, 8))

# here I am drawing a greyscale image
image = img.imread(file_name)
fig.add_subplot(1, 2, 1).imshow(image, cmap=cm.Greys_r)
fig.tight_layout()
plt.axis('off')

# and here I am drawing a scatterplot based on this image.
X, Y = image.shape
x, y = np.nonzero(image)
fig.add_subplot(1, 2, 2).scatter(y - Y, -x)
fig.tight_layout()

问题是当我绘制它们时,它们的高度不同。怎么才能让高度一样呢? enter image description here

寻找设置高度 matplotlib 没有给出合适的结果。

最佳答案

您需要将关键字 aspect='auto' 添加到 imshow 调用中。所以它应该看起来像:

fig.add_subplot(1, 2, 1).imshow(image, cmap=cm.Greys_r, aspect='auto')

来自文档:

aspect : [‘auto’ | ‘equal’ | scalar], optional, default: None

If ‘auto’, changes the image aspect ratio to match that of the axes.

If ‘equal’, and extent is None, changes the axes aspect ratio to match that of the image. If extent is not None, the axes aspect ratio is changed to match that of the extent.

If None, default to rc image.aspect value.

关于python - 如何使图像和散点图具有相同的高度?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32434501/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com