我使用以下代码尝试使用matplotlib生成等高线图。
脚本代码:
#!/usr/bin/python
from mpl_toolkits.basemap import Basemap
import matplotlib.pyplot as plt
import matplotlib.cm as cm
import matplotlib.colors as colors
import numpy as np
from matplotlib.mlab import griddata
a = np.loadtxt('/home/weather/site_data')
def show_map(self, a):
# 'a' is of the format [(lats, lons, data), (lats, lons, data)... (lats, lons, data)]
lats = [ x[0] for x in a ]
lons = [ x[1] for x in a ]
data = [ x[2] for x in a ]
lat_min = min(lats)
lat_max = max(lats)
lon_min = min(lons)
lon_max = max(lons)
data_min = min(data)
data_max = max(data)
spatial_resolution = 0.5
fig = plt.figure()
x = np.array(lons)
y = np.array(lats)
z = np.array(data)
xinum = (lon_max - lon_min) / spatial_resolution
yinum = (lat_max - lat_min) / spatial_resolution
xi = np.linspace(lon_min, lon_max + spatial_resolution, xinum) # same as [lon_min:spatial_resolution:lon_max] in matlab
yi = np.linspace(lat_min, lat_max + spatial_resolution, yinum) # same as [lat_min:spatial_resolution:lat_max] in matlab
xi, yi = np.meshgrid(xi, yi)
zi = griddata(x, y, z, xi, yi)
m = Basemap(
projection = 'merc',
llcrnrlat=lat_min, urcrnrlat=lat_max,
llcrnrlon=lon_min, urcrnrlon=lon_max,
rsphere=6371200., resolution='l', area_thresh=10000
)
m.drawcoastlines()
m.drawstates()
m.drawcountries()
lat, lon = m.makegrid(zi.shape[1], zi.shape[0])
x,y = m(lat, lon)
m.contourf(x, y, zi)
plt.show()
数据:
[(45.43, -75.65, 75.9), (44.30, -73.63, 85.2), (45.76, -65.76, 68.2)]
我试图弄清楚我输入的数据是否错误,或者是否还有其他潜在问题。
我一直收到以下错误代码:
ValueError: could not convert string to float: [(45.43,
我自己尝试修复它并认为我通过删除所有括号和逗号来解决它,但是,脚本甚至没有绘制图像。