Matplotlib/Cartopy: pcolormesh error: ValueError: too many values to unpack (expected 3)
Matplotlib/Cartopy: pcolormesh error: ValueError: too many values to unpack (expected 3)
我在绘图时遇到了问题,尝试使用 pcolormesh 通过 matplotlib/cartopy 绘制 lat/lon 数据。
我有下面的代码块和错误。我似乎无法理解特定错误的来源:pcolormesh 函数发现太多值无法解包。
Python3.8,Matplotlib 3.4.2,Cartopy 0.17
我还在学习这个过程...但这看起来应该很简单,但我似乎无法解决这个错误。如有任何帮助,我们将不胜感激!
import matplotlib.pyplot as plt
import xarray as xr
import numpy as np
import cartopy.crs as ccrs
import cartopy.feature as cfeat
from cartopy.feature import ShapelyFeature
import cartopy
import geopandas as gpd
import matplotlib.ticker as mticker
from cartopy.mpl.gridliner import LONGITUDE_FORMATTER, LATITUDE_FORMATTER
import cmocean as cm
##Open weather data file (grib2 format) via xarray, select data from proper level
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)
##Select temperature variable from specific level
t500 = ds_z500['t']
lats = ds_z500['latitude']
lons = ds_z500['longitude']
latdata = lats.values
londata = lons.values
data = t500.values
fig = plt.figure(figsize=(10,10))
ax = fig.add_subplot(1,1,1, projection=ccrs.PlateCarree())
ax.pcolormesh(londata, latdata, data, transform=ccrs.PlateCarree(), cmap=cm.cm.thermal, vmin=250, vmax=330)
##This contourf function works fine.
##ax.contourf(ds_z500['longitude'], ds_z500['latitude'], ds_z500['t'], transform=ccrs.PlateCarree())
ax.add_feature(cfeat.COASTLINE.with_scale('50m'),edgecolor='black')
ax.add_feature(cfeat.LAKES.with_scale('50m'), edgecolor='black',facecolor='none')
ax.add_feature(cfeat.BORDERS.with_scale('50m'),edgecolor='black')
plt.savefig('test_grib_colormesh.png')
***************************************************************
/home/fewx/anaconda3/lib/python3.8/site-packages/cartopy/mpl/geoaxes.py:1491: MatplotlibDeprecationWarning: shading='flat' when X and Y have the same dimensions as C is deprecated since 3.3. Either specify the corners of the quadrilaterals with X and Y, or pass shading='auto', 'nearest' or 'gouraud', or set rcParams['pcolor.shading']. This will become an error two minor releases later.
X, Y, C = self._pcolorargs('pcolormesh', *args, allmatch=allmatch)
Traceback (most recent call last):
File "work_with_grib_data.py", line 132, in <module>
ax.pcolormesh(londata, latdata, data, transform=ccrs.PlateCarree(), cmap=cm.cm.thermal, vmin=250, vmax=330)
File "/home/fewx/anaconda3/lib/python3.8/site-packages/cartopy/mpl/geoaxes.py", line 1459, in pcolormesh
result = self._pcolormesh_patched(*args, **kwargs)
File "/home/fewx/anaconda3/lib/python3.8/site-packages/cartopy/mpl/geoaxes.py", line 1491, in _pcolormesh_patched
X, Y, C = self._pcolorargs('pcolormesh', *args, allmatch=allmatch)
ValueError: too many values to unpack (expected 3)
这与以下问题相同:Plotting with matplotlib/cartopy: ValueError: too many values to unpack (expected 3)
这是 cartopy 版本 0.17 和 matplotlib >3.3 的问题。
这个问题在cartopy 0.19版本中已经修复,所以更新一下应该没问题。
我在绘图时遇到了问题,尝试使用 pcolormesh 通过 matplotlib/cartopy 绘制 lat/lon 数据。
我有下面的代码块和错误。我似乎无法理解特定错误的来源:pcolormesh 函数发现太多值无法解包。
Python3.8,Matplotlib 3.4.2,Cartopy 0.17
我还在学习这个过程...但这看起来应该很简单,但我似乎无法解决这个错误。如有任何帮助,我们将不胜感激!
import matplotlib.pyplot as plt
import xarray as xr
import numpy as np
import cartopy.crs as ccrs
import cartopy.feature as cfeat
from cartopy.feature import ShapelyFeature
import cartopy
import geopandas as gpd
import matplotlib.ticker as mticker
from cartopy.mpl.gridliner import LONGITUDE_FORMATTER, LATITUDE_FORMATTER
import cmocean as cm
##Open weather data file (grib2 format) via xarray, select data from proper level
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)
##Select temperature variable from specific level
t500 = ds_z500['t']
lats = ds_z500['latitude']
lons = ds_z500['longitude']
latdata = lats.values
londata = lons.values
data = t500.values
fig = plt.figure(figsize=(10,10))
ax = fig.add_subplot(1,1,1, projection=ccrs.PlateCarree())
ax.pcolormesh(londata, latdata, data, transform=ccrs.PlateCarree(), cmap=cm.cm.thermal, vmin=250, vmax=330)
##This contourf function works fine.
##ax.contourf(ds_z500['longitude'], ds_z500['latitude'], ds_z500['t'], transform=ccrs.PlateCarree())
ax.add_feature(cfeat.COASTLINE.with_scale('50m'),edgecolor='black')
ax.add_feature(cfeat.LAKES.with_scale('50m'), edgecolor='black',facecolor='none')
ax.add_feature(cfeat.BORDERS.with_scale('50m'),edgecolor='black')
plt.savefig('test_grib_colormesh.png')
***************************************************************
/home/fewx/anaconda3/lib/python3.8/site-packages/cartopy/mpl/geoaxes.py:1491: MatplotlibDeprecationWarning: shading='flat' when X and Y have the same dimensions as C is deprecated since 3.3. Either specify the corners of the quadrilaterals with X and Y, or pass shading='auto', 'nearest' or 'gouraud', or set rcParams['pcolor.shading']. This will become an error two minor releases later.
X, Y, C = self._pcolorargs('pcolormesh', *args, allmatch=allmatch)
Traceback (most recent call last):
File "work_with_grib_data.py", line 132, in <module>
ax.pcolormesh(londata, latdata, data, transform=ccrs.PlateCarree(), cmap=cm.cm.thermal, vmin=250, vmax=330)
File "/home/fewx/anaconda3/lib/python3.8/site-packages/cartopy/mpl/geoaxes.py", line 1459, in pcolormesh
result = self._pcolormesh_patched(*args, **kwargs)
File "/home/fewx/anaconda3/lib/python3.8/site-packages/cartopy/mpl/geoaxes.py", line 1491, in _pcolormesh_patched
X, Y, C = self._pcolorargs('pcolormesh', *args, allmatch=allmatch)
ValueError: too many values to unpack (expected 3)
这与以下问题相同:Plotting with matplotlib/cartopy: ValueError: too many values to unpack (expected 3)
这是 cartopy 版本 0.17 和 matplotlib >3.3 的问题。 这个问题在cartopy 0.19版本中已经修复,所以更新一下应该没问题。