您应该插值缺失的数据,我在我的一个项目中使用了以下方法:
#create regular grid
xi, yi = np.linspace(x.min(), x.max(), 100), np.linspace(y.min(), y.max(), 100)
xi, yi = np.meshgrid(xi, yi)
#interpolate missing data
rbf = scipy.interpolate.Rbf(x, y, z, function='linear')
zi = rbf(xi, yi)
稍微更详细地说明:
import matplotlib.pyplot as plt
import numpy as np
import scipy
def nonuniform_imshow(x, y, z, aspect=1, cmap=plt.cm.rainbow):
# Create regular grid
xi, yi = np.linspace(x.min(), x.max(), 100), np.linspace(y.min(), y.max(), 100)
xi, yi = np.meshgrid(xi, yi)
# Interpolate missing data
rbf = scipy.interpolate.Rbf(x, y, z, function='linear')
zi = rbf(xi, yi)
_, ax = plt.subplots(figsize=(6, 6))
hm = ax.imshow(zi, interpolation='nearest', cmap=cmap,
extent=[x.min(), x.max(), y.max(), y.min()])
ax.scatter(x, y)
ax.set_aspect(aspect)
return hm
heatmap = nonuniform_imshow(x, y, z)
plt.colorbar(heatmap)
plt.show()