问题是imshow(z_list, ...)将期望z_list是一个(n,m)类型的数组,基本上是一个值网格。要使用imshow函数,需要为每个网格点设置Z值,可以通过收集更多数据或插值来实现。
下面是一个将数据与线性插值结合使用的示例: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')
您可以看到它在您的采样点处显示正确的值(由x_list和y_list指定,由半圆表示),但由于插值的性质和采样点的数量较少,它在其他位置的变化要大得多。