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

标签 python matplotlib

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

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/

相关文章:

python - 使用 pytest 用于 Python 项目的正确文件夹结构是什么?

python - Matplotlib 离线绘图

python - 未知错误的 Matplotlib 散点图

python - 如何在 python 中捕获任何异常并打印堆栈跟踪 (DJANGO)

python - Numpy 无法在内存中存储大于 1GB 的矩阵

python - 即使预期返回 'None',仍继续返回 'True'

python - python中特定几何图形的二维等值线图

python - 如何将 seaborn/matplotlib 轴刻度标签从数字格式化为数千或数百万? (125,436 至 125.4K)

python - 如何将等高线图(matplotlib)转换为带标题的 FITS 格式?

python - 在 Windows 7 下从 python 启动时找不到 R 包