<p>从<a href="https://matplotlib.org/3.1.1/api/_as_gen/matplotlib.pyplot.imshow.html" rel="nofollow noreferrer">^{<cd1>}</a>官方指南中,我们知道纵横比控制轴的纵横比。用我的话说,纵横比就是x<strong>单位</strong>和y<strong>单位</strong>的比值。大多数时候,我们希望保持它为1,因为我们不想无意中歪曲数字。然而,确实有一些情况下,我们需要指定方面a的值,而不是1。发问者提供了一个很好的例子,x轴和y轴可能有不同的物理单位。假设x以km为单位,y以m为单位。因此,对于10x10数据,范围应为[0,10km,0,10m]=[0,10000m,0,10m]。在这种情况下,如果我们继续使用default aspect=1,那么图的质量就非常糟糕。因此,我们可以指定aspect=1000来优化我们的图形。下面的代码说明了这种方法。</p>
<pre><code>%matplotlib inline
import numpy as np
import matplotlib.pyplot as plt
rng=np.random.RandomState(0)
data=rng.randn(10,10)
plt.imshow(data, origin = 'lower', extent = [0, 10000, 0, 10], aspect = 1000)
</code></pre>
<p><a href="https://i.stack.imgur.com/Tc3Q8.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/Tc3Q8.png" alt="enter image description here"/></a></p>
<p>不过,我认为还有一种选择可以满足提问者的要求。我们可以将范围设置为[0,10,0,10]并添加额外的xy轴标签来表示单位。代码如下。</p>
<pre><code>plt.imshow(data, origin = 'lower', extent = [0, 10, 0, 10])
plt.xlabel('km')
plt.ylabel('m')
</code></pre>
<p><a href="https://i.stack.imgur.com/tXYl5.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/tXYl5.png" alt="enter image description here"/></a></p>
<p>为了做出一个正确的数字,我们应该始终记住<code>x_max-x_min = x_res * data.shape[1]</code>和<code>y_max - y_min = y_res * data.shape[0]</code>,其中<code>extent = [x_min, x_max, y_min, y_max]</code>。默认情况下,<code>aspect = 1</code>,表示单位像素为正方形。对于具有不同值的x_res和y_res,此默认行为也可以正常工作。扩展前面的例子,假设x_res是1.5,而y_res是1。因此范围应等于[0,15,0,10]。使用默认方面,我们可以有矩形颜色像素,而单位像素仍然是正方形!</p>
<pre><code>plt.imshow(data, origin = 'lower', extent = [0, 15, 0, 10])
# Or we have similar x_max and y_max but different data.shape, leading to different color pixel res.
data=rng.randn(10,5)
plt.imshow(data, origin = 'lower', extent = [0, 5, 0, 5])
</code></pre>
<p><a href="https://i.stack.imgur.com/p30iK.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/p30iK.png" alt="enter image description here"/></a>
<a href="https://i.stack.imgur.com/qMkEg.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/qMkEg.png" alt="enter image description here"/></a></p>
<p>彩色像素的方面是<code>x_res / y_res</code>。将其纵横比设置为单位像素的纵横比(即<code>aspect = x_res / y_res = ((x_max - x_min) / data.shape[1]) / ((y_max - y_min) / data.shape[0])</code>)将始终提供方形颜色像素。我们可以改变aspect=1.5,使x轴单位是y轴单位的1.5倍,从而得到一个正方形的彩色像素和正方形的整个图形,而不是矩形的像素单位。显然,这通常是不被接受的。</p>
<pre><code>data=rng.randn(10,10)
plt.imshow(data, origin = 'lower', extent = [0, 15, 0, 10], aspect = 1.5)
</code></pre>
<p><a href="https://i.stack.imgur.com/u5XXV.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/u5XXV.png" alt="enter image description here"/></a></p>
<p>最不希望出现的情况是,将aspect设置为任意值,如1.2,这既不会导致平方单位像素,也不会导致平方颜色像素。</p>
<pre><code>plt.imshow(data, origin = 'lower', extent = [0, 15, 0, 10], aspect = 1.2)
</code></pre>
<p><a href="https://i.stack.imgur.com/Vr1AQ.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/Vr1AQ.png" alt="enter image description here"/></a></p>
<p>长话短说,只要设置正确的范围,让matplotlib为我们做剩下的事情就足够了(即使x琰res!=是的)!只有在必要时才改变方面。</p>