我已经阅读了这个问题 - Overlay shape file on matplotlib并且我不确定它是否适用于我,但我有一个我国的shapefile,我想将GRIB文件中的降水数据叠加到该shapefile上。这是使用pygrib和matplotlib。我对使用Basemap.coastlines()和Basemap.countries()不感兴趣,因为它在shapefile中可用。我想用我自己的shapefile覆盖它。我该怎么做?
#!/usr/bin/python3.4
import numpy as np
import pygrib
import shapefile
import matplotlib.pyplot as plt
from mpl_toolkits.basemap import Basemap
grbs = pygrib.open('00020000')
grb = grbs.select(name='Total Precipitation')[0]
data = grb.values
lat,lon = grb.latlons()
m=Basemap(projection='mill',lat_ts=10,llcrnrlon=lon.min(), \
urcrnrlon=lon.max(),llcrnrlat=lat.min(),urcrnrlat=lat.max(), \
resolution='c')
m.readshapefile("adm3.shp")
x,y=m(lon,lat)
cs = m.pcolormesh(x,y,data,shading='flat',cmap=plt.cm.jet)
m.drawparallels(np.arange(-90.,80.,5.),labels=[1,0,0,0])
m.drawmeridians(np.arange(-180.,180.,5.),labels=[0,0,0,1])
plt.colorbar(cs,orientation='vertical')
plt.title('Precipitation Plot')
plt.show()
答案 0 :(得分:1)
我不确定我是否理解,因为您的代码似乎基本上可行。我做了一些改动:
resolution
kwarg Basemap
可以设置为None
,因为您不会使用内置海岸线
m.readshapefile
需要name
参数使用
import numpy as np
import matplotlib.pyplot as plt
from mpl_toolkits.basemap import Basemap
grbs = pygrib.open('00020000.grb')
grb = grbs.select(name='Total Precipitation')[0]
data = grb.values
lat, lon = grb.latlons()
m = Basemap(projection='mill', lat_ts=10,
llcrnrlon=lon.min(),
urcrnrlon=lon.max(),
llcrnrlat=lat.min(),
urcrnrlat=lat.max(),
resolution=None)
m.readshapefile("adm3", "countryname", color="red", linewidth=3)
x, y = m(lon,lat)
cs = m.pcolormesh(x,y,data,shading='flat',cmap=plt.cm.jet)
m.drawparallels(np.arange(-90.,80.,5.),labels=[1,0,0,0])
m.drawmeridians(np.arange(-180.,180.,5.),labels=[0,0,0,1])
plt.colorbar(cs,orientation='vertical')
plt.title('Precipitation Plot')
plt.show()