<p>问题是<code>imshow(z_list, ...)</code>将期望<code>z_list</code>是一个<code>(n,m)</code>类型的数组,基本上是一个值网格。要使用imshow函数,需要为每个网格点设置Z值,可以通过收集更多数据或插值来实现。</p>
<p>下面是一个将数据与线性插值结合使用的示例:</p>
<pre><code>from scipy.interpolate import interp2d
# f will be a function with two arguments (x and y coordinates),
# but those can be array_like structures too, in which case the
# result will be a matrix representing the values in the grid
# specified by those arguments
f = interp2d(x_list,y_list,z_list,kind="linear")
x_coords = np.arange(min(x_list),max(x_list)+1)
y_coords = np.arange(min(y_list),max(y_list)+1)
Z = f(x_coords,y_coords)
fig = plt.imshow(Z,
extent=[min(x_list),max(x_list),min(y_list),max(y_list)],
origin="lower")
# Show the positions of the sample points, just to have some reference
fig.axes.set_autoscale_on(False)
plt.scatter(x_list,y_list,400,facecolors='none')
</code></pre>
<p><a href="https://i.stack.imgur.com/wtKLd.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/wtKLd.png" alt="enter image description here"/></a></p>
<p>您可以看到它在您的采样点处显示正确的值(由<code>x_list</code>和<code>y_list</code>指定,由半圆表示),但由于插值的性质和采样点的数量较少,它在其他位置的变化要大得多。</p>