Python Matplotlib imshow() 拉伸到“适合宽度”

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12806481/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-18 11:54:57  来源:igfitidea点击:

Matplotlib imshow() stretch to "fit width"

pythonmatplotlib

提问by ajwood

I've got an image, and a measure associated with each column of its pixels. I'm using pyplotto create a figure with the image on top, and a plot of the column measurements below. I'm using something like this:

我有一个图像,以及与其像素的每一列相关联的度量。我正在使用pyplot顶部的图像创建一个图形,以及下面的列测量图。我正在使用这样的东西:

import numpy as np
import matplotlib.pyplot as plt

A = np.random.rand(34*52).reshape(34,52)
means = np.average(A,axis=0)

plt.figure()

plt.subplot(2,1,1)
plt.imshow(A, interpolation='nearest' )

plt.subplot(2,1,2)
plt.plot(means)

plt.show()

How can I stretch the image's width to the match that of the plots. That way, when looking at the measurements in the plot, the souce pixels will be in a column directly above it.

如何拉伸图像的宽度以匹配绘图的宽度。这样,当查看图中的测量值时,源像素将位于其正上方的列中。

采纳答案by ajwood

Turns out that it's as simple as giving aspect='auto'to the imshowcall.

事实证明,它是那样简单给aspect='auto'imshow电话。

plt.imshow(A, interpolation='nearest', aspect='auto')