Python3.8:MetPy 问题:grib2 文件的声明性绘图绘制空白地图

Python3.8: MetPy issue: declarative plotting of grib2 file plotting blank map

自学 MetPy 中的声明式绘图功能,并不断遇到障碍。这是因为在通过 xarray/cfgrib 打开后,尝试将声明性绘图包应用于 grib2 文件。数据 看起来 健康,但似乎没有传递和绘制实际温度数据。当我绘制它时,我得到一张空白地图。

我有下面的压缩代码,以及一些数据检查的打印输出,表明数据似乎没问题。

我错过了什么吗? (我确定我是,但我想知道什么?)

谢谢!

import matplotlib.pyplot as plt
import xarray as xr
import numpy as np
import pygrib
import cartopy.crs as ccrs
import cartopy.feature as cfeat
import cartopy
from datetime import datetime, timedelta
import io
from metpy.units import units
from metpy.plots import ImagePlot, MapPanel, PanelContainer

##Open GFS grib2 file, initialized 2021060700, and pull data from hPa level designation.
ds = xr.open_dataset('/fewxops/Tom/learn_python/data/grib2/gfs.t00z.pgrb2.0p25.f024', engine='cfgrib', filter_by_keys={'typeOfLevel': 'isobaricInhPa'})

##Select individual level (from designated 'type of level')
ds_z500=ds.sel(isobaricInhPa=500)

##Create base image via MetPy
img = ImagePlot()
img.data = ds_z500
img.field = 't'
img.colormap = 'plasma'

#Create map panel (e.g. subplot in matplotlib)
panel = MapPanel()
panel.area = 'us'
#panel.layers = ['states']
panel.title = 'GFS 500mb Temp Forecast Example'
panel.plots = [img]

##Create panel container (e.g. figure in matplotlib)
pc = PanelContainer()
pc.size = (10,8)
pc.panels = [panel]
pc.show()

*******************************
home/fewx/anaconda3/lib/python3.8/site-packages/metpy/xarray.py:349: UserWarning: More than one time coordinate present for variable "t".
  warnings.warn('More than one ' + axis + ' coordinate present for variable'
Found valid latitude/longitude coordinates, assuming latitude_longitude for projection grid_mapping variable
print(ds_z500)
<xarray.Dataset>
Dimensions:        (latitude: 721, longitude: 1440)
Coordinates:
    time           datetime64[ns] ...
    step           timedelta64[ns] ...
    isobaricInhPa  float64 500.0
  * latitude       (latitude) float64 90.0 89.75 89.5 ... -89.5 -89.75 -90.0
  * longitude      (longitude) float64 0.0 0.25 0.5 0.75 ... 359.2 359.5 359.8
    valid_time     datetime64[ns] ...
Data variables:
    gh             (latitude, longitude) float32 ...
    t              (latitude, longitude) float32 ...
    r              (latitude, longitude) float32 ...
    q              (latitude, longitude) float32 ...
    w              (latitude, longitude) float32 ...
    wz             (latitude, longitude) float32 ...
    u              (latitude, longitude) float32 ...
    v              (latitude, longitude) float32 ...
    absv           (latitude, longitude) float32 ...
    o3mr           (latitude, longitude) float32 ...
Attributes:
    GRIB_edition:            2
    GRIB_centre:             kwbc
    GRIB_centreDescription:  US National Weather Service - NCEP
    GRIB_subCentre:          0
    Conventions:             CF-1.7
    institution:             US National Weather Service - NCEP
    history:                 2021-06-15T08:05 GRIB to CDM+CF via cfgrib-0.9.9...


print(ds_z500['t'].values)
[[245.93057 245.93057 245.93057 ... 245.93057 245.93057 245.93057]
 [245.85057 245.84056 245.84056 ... 245.85057 245.85057 245.85057]
 [245.72057 245.72057 245.71057 ... 245.73056 245.73056 245.72057]
 ...
 [226.94057 226.94057 226.94057 ... 226.94057 226.94057 226.94057]
 [226.96057 226.96057 226.96057 ... 226.96057 226.96057 226.96057]
 [226.88057 226.88057 226.88057 ... 226.88057 226.88057 226.88057]]

GFS Example

您没有遗漏任何东西,您只是 运行 限制了 ImagePlot 中试图帮助 CartoPy 的一些代码——特别是关于使用 imshow 和 lat/lon数据。您的示例代码 应该 可以正常工作,ContourPlot 也可以。我已打开 an issue 以进一步调查如何在下一个版本中解决此问题。

与此同时,一种解决方法是在绘图之前对数据进行子集化,以消除 MetPy 有问题的“帮助”包装(大约 -180/+180 和 0/360 经度):

# The order in these slices needs to match that of the data
ds_z500 = ds_z500.metpy.sel(longitude=slice(250, 315), latitude=slice(60, 10))