I'm working on a basic plot of data on a map via xarray, matplotlib and cartopy. My problem comes from simply trying to change the x and y axis ticks to the corresponding lat/lon values.
The problem arises from trying to apply set_yaxis, which is in the final 3 lines of the sample code snippet below.
The error returns:
Traceback (most recent call last): File "work_with_grib_data.py", line 163, in ax.set_yticks(np.arange(-90,91,30))
File "/home/fewx/anaconda3/lib/python3.8/site-packages/cartopy/mpl/geoaxes.py", line 829, in set_yticks return super(GeoAxes, self).set_yticks(yticks, minor)
File "/home/fewx/anaconda3/lib/python3.8/site-packages/matplotlib/axes/_base.py", line 73, in wrapper return get_method(self)(*args, **kwargs)
TypeError: set_ticks() takes 2 positional arguments but 3 were given
I get that the first element is 'self', but how is matplotlib seeing a third element here? This has to be a simple error, but what am I overlooking?
Gory details: Python3.8, Matplotlib 3.4, Cartopy 0.17
Thanks for your help!
Basic code:
#!/home/fewx/anaconda3/bin/python3.8
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
from cartopy.io.shapereader import Reader
from cartopy.feature import ShapelyFeature
import cartopy
import geopandas as gpd
ds= xr.open_dataset('/fewxops/Tom/learn_python/data/grib2/gfs.t00z.pgrb2.0p25.f024', engine='cfgrib', filter_by_keys={'typeOfLevel': 'isobaricInhPa'})
ds_z500=ds.sel(isobaricInhPa=500)
fig, ax = plt.subplots(figsize=(10,10))
ax = plt.axes(projection=ccrs.PlateCarree())
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')
##Add ticks for latitude
ax.set_yticks(np.arange(-90,91,30))
************************************************
Traceback (most recent call last):
File "work_with_grib_data.py", line 163, in <module>
ax.set_yticks(np.arange(-90,91,30))
File "/home/fewx/anaconda3/lib/python3.8/site-packages/cartopy/mpl/geoaxes.py", line 829, in set_yticks
return super(GeoAxes, self).set_yticks(yticks, minor)
File "/home/fewx/anaconda3/lib/python3.8/site-packages/matplotlib/axes/_base.py", line 73, in wrapper
return get_method(self)(*args, **kwargs)
TypeError: set_ticks() takes 2 positional arguments but 3 were given
UPDATE I've engineered around this problem, using the gridline feature of Cartopy, and simply not plotting the gridlines. See the code below.
I'm wondering if my Matplotlib (3.4) and Cartopy (0.17) are to blame here, and causing some friction. Trying to upgrade Cartopy to (0.19) leads to the dreaded 'frozen solve' in Conda, so perhaps I'll try to downgrade Matplotlib a version to see if that happens to fix the set_yaxis ticks issue.
#!/home/fewx/anaconda3/bin/python3.8
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
from cartopy.io.shapereader import Reader
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
ds = xr.open_dataset('/fewxops/Tom/learn_python/data/grib2/gfs.t00z.pgrb2.0p25.f024', engine='cfgrib', filter_by_keys={'typeOfLevel': 'isobaricInhPa'})
ds_z500=ds.sel(isobaricInhPa=500)
fig = plt.figure(figsize=(10,10))
ax = fig.add_subplot(1,1,1, projection=ccrs.PlateCarree())
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')
gl = ax.gridlines(crs=ccrs.PlateCarree(), draw_labels=True)
gl.xlabels_top = False
gl.ylabels_left = False
gl.xlines = False
gl.ylines = False
gl.xlocator = mticker.FixedLocator([-180, -120, -60, 0, 60, 120, 180])
gl.ylocator = mticker.FixedLocator([-90, -45, 0, 45, 90])
gl.xformatter = LONGITUDE_FORMATTER
gl.yformatter = LATITUDE_FORMATTER
plt.savefig('test_grib.png')