【发布时间】:2019-05-14 17:37:21
【问题描述】:
我有一个包含无序数据点的字典列表:
print(data)
[{'ID': 1, 'Longitude': 18.6081, 'Latitude': 50.0977, 'Cost': -1.0},
{'ID': 2, 'Longitude': 18.6091, 'Latitude': 50.197700000000005, 'Cost': 4},
{'ID': 3, 'Longitude': 18.6081, 'Latitude': 50.297700000000006,'Cost':4},....]
我想使用 matplotlib 将 'cost' 函数的值绘制为 countorf。
到目前为止,我已经测试了以下代码:
lat = []
lon = []
cost = []
for dataPoint in data:
if dataPoint["Cost"] == -1:
continue
lon.append(dataPoint["Longitude"])
lat.append(dataPoint["Latitude"])
cost.append(dataPoint["Cost"])
xi = np.linspace(min(lon), max(lon))
yi = np.linspace(min(lat), max(lat))
zi = griddata(lon, lat, cost, xi, yi, interp='linear')
#make a plot
plt.contourf(xi, yi, zi, 100)
plt.colorbar()
plt.show()
上面的代码有效,但它在数据点之间插值,我想做的就是在平面上绘制数据数组中的所有点,颜色对应于它的“成本”值。我该怎么做?
【问题讨论】:
标签: matplotlib python-3.7 contourf