数据不适合于底图

2024-06-16 11:20:45 发布

您现在位置:Python中文网/ 问答频道 /正文

当我尝试使用Grib文件中的Basemap打印数据时,该地图与正在打印的数据不匹配。我发布了下面使用的代码和下面输出图像的链接。我认为投影类型可能是问题所在。 我还试着在保持代码其余部分不变的情况下使投影为圆柱形,但这也不起作用(尽管看起来好一点),我还为此贴了一个到输出图像的链接。也许这有助于形象化什么样的投影将适合数据? 对于您认为可能出现问题的任何想法,我们将不胜感激

from pprint import pprint
import pygrib
from mpl_toolkits.basemap import Basemap
import numpy as np
import matplotlib
matplotlib.use('Agg')
import matplotlib.pyplot as plt
from ncepgrib2 import Grib2Decode


file = 'hrrr.t00z.wrfsfcf00.grib2'
gr = pygrib.open(file)


msg = gr[32] #Temperature values in Kelvin
#print(Grib2Decode(msg.tostring(),gribmsg=True))#Similar to printing all info for netCDF file
Temp = msg.values

lat, lon = msg.latlons()

m=Basemap(projection='lcc',llcrnrlon=lon.min(), \
  urcrnrlon=lon.max(),llcrnrlat=lat.min(),urcrnrlat=lat.max(), \
  lat_0 = float(msg['latitudeOfFirstGridPointInDegrees']),lon_0 =float(msg['longitudeOfFirstGridPointInDegrees']) ,resolution='c')

x,y = m(lon,lat)


                                                                                                                                                                                                                                                                                                                                                                                          #m = Basemap(width=11297120.0,height=8959788.0,
#           resolution='c',projection='lcc',\
#            lat_ts=40,lat_0=lat_0,lon_0=lon_0)



fig = plt.figure(figsize = (18.6,10.5))
cs = m.pcolormesh(x,y,(Temp-273.15)*9/5 +32,cmap = plt.cm.jet)
m.drawcoastlines()
m.drawstates()
m.drawcountries()
plt.colorbar(cs,orientation='vertical')
plt.title('Temperature F')
plt.savefig('plot')
plt.show()                                                                                           

Image of output using lcc projectionImage of output using cylindrical projectionLink to HRRR data containing Grib file. All are in the exact same format, projection, etc... so you could download any one of them and get the same results (just different Temperature values)


Tags: 数据fromimportmatplotlibpltmsgfile投影