Search code examples
pythonmatplotlibcartopy

Matplotlib/Cartopy: pcolormesh error: ValueError: too many values to unpack (expected 3)


I'm having plotting trouble, trying to use pcolormesh to plot lat/lon data via matplotlib/cartopy.

I have the block of code and error below. I cannot seem to understand where the particular error is coming from: that the pcolormesh function is finding too many values to unpack.

Python3.8, Matplotlib 3.4.2, Cartopy 0.17

I'm still learning this process...but this seems like this should be straightforward, yet I cannot seem to solve this error. Any help would be appreciated!

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)






Solution

  • This is same issue as: Plotting with matplotlib/cartopy: ValueError: too many values to unpack (expected 3)

    The this is issue with cartopy version 0.17 and matplotlib >3.3. This is fixed in cartopy version 0.19, so just update and it should be fine.