我正在绘制两个不同的子图。一个是图像,另一个是散点图。
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()
问题是当我绘制它们时,它们的高度不同。怎么才能让高度一样呢?
寻找设置高度 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/