You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I am encountering an error while attempting to plot xarray fields using cartopy & matplotlib. [I'm posting here first but I'm not sure if the error is due to xarray, cartopy, or matplotlib].
Minimal example — the example is as focused as reasonably possible to demonstrate the underlying issue in xarray.
Complete example — the example is self-contained, including all data and the text of any traceback.
Verifiable example — the example copy & pastes into an IPython prompt or Binder notebook, returning the result.
New issue — a search of GitHub Issues suggests this is not a duplicate.
Relevant log output
TypeErrorTraceback (mostrecentcalllast)
InputIn [12], in<cellline: 11>()
9ax.coastlines()
10ax.gridlines()
--->11sst.plot(ax=ax, transform=ccrs.PlateCarree(),
12vmin=2, vmax=30)
File~/miniconda3/envs/AQUATIC/lib/python3.10/site-packages/xarray/plot/plot.py:868, in_PlotMethods.__call__(self, **kwargs)
867def__call__(self, **kwargs):
-->868returnplot(self._da, **kwargs)
File~/miniconda3/envs/AQUATIC/lib/python3.10/site-packages/xarray/plot/plot.py:334, inplot(darray, row, col, col_wrap, ax, hue, rtol, subplot_kws, **kwargs)
330plotfunc=hist332kwargs["ax"] =ax-->334returnplotfunc(darray, **kwargs)
File~/miniconda3/envs/AQUATIC/lib/python3.10/site-packages/xarray/plot/plot.py:1212, in_plot2d.<locals>.newplotfunc(darray, x, y, figsize, size, aspect, ax, row, col, col_wrap, xincrease, yincrease, add_colorbar, add_labels, vmin, vmax, cmap, center, robust, extend, levels, infer_intervals, colors, subplot_kws, cbar_ax, cbar_kwargs, xscale, yscale, xticks, yticks, xlim, ylim, norm, **kwargs)
1208raiseValueError("plt.imshow's `aspect` kwarg is not available in xarray")
1210ax=get_axis(figsize, size, aspect, ax, **subplot_kws)
->1212primitive=plotfunc(
1213xplt,
1214yplt,
1215zval,
1216ax=ax,
1217cmap=cmap_params["cmap"],
1218vmin=cmap_params["vmin"],
1219vmax=cmap_params["vmax"],
1220norm=cmap_params["norm"],
1221**kwargs,
1222 )
1224# Label the plot with metadata1225ifadd_labels:
File~/miniconda3/envs/AQUATIC/lib/python3.10/site-packages/xarray/plot/plot.py:1478, inpcolormesh(x, y, z, ax, xscale, yscale, infer_intervals, **kwargs)
1475y=_infer_interval_breaks(y, axis=1, scale=yscale)
1476y=_infer_interval_breaks(y, axis=0, scale=yscale)
->1478primitive=ax.pcolormesh(x, y, z, **kwargs)
1480# by default, pcolormesh picks "round" values for bounds1481# this results in ugly looking plots with lots of surrounding whitespace1482ifnothasattr(ax, "projection") andx.ndim==1andy.ndim==1:
1483# not a cartopy geoaxisFile~/miniconda3/envs/AQUATIC/lib/python3.10/site-packages/cartopy/mpl/geoaxes.py:318, in_add_transform.<locals>.wrapper(self, *args, **kwargs)
313raiseValueError(f'Invalid transform: Spherical {func.__name__} '314'is not supported - consider using '315'PlateCarree/RotatedPole.')
317kwargs['transform'] =transform-->318returnfunc(self, *args, **kwargs)
File~/miniconda3/envs/AQUATIC/lib/python3.10/site-packages/cartopy/mpl/geoaxes.py:1799, inGeoAxes.pcolormesh(self, *args, **kwargs)
1797result=matplotlib.axes.Axes.pcolormesh(self, *args, **kwargs)
1798# Wrap the quadrilaterals if necessary->1799result=self._wrap_quadmesh(result, **kwargs)
1800# Re-cast the QuadMesh as a GeoQuadMesh to enable future wrapping1801# updates to the collection as well.1802result.__class__=cartopy.mpl.geocollection.GeoQuadMeshFile~/miniconda3/envs/AQUATIC/lib/python3.10/site-packages/cartopy/mpl/geoaxes.py:1982, inGeoAxes._wrap_quadmesh(self, collection, **kwargs)
1968# Plot all of the wrapped cells.1969# `pcolor` only draws polygons where the data is not1970# masked, so this will only draw a limited subset of
(...)
1978# fill in the proper data later with set_array()1979# calls.1980pcolor_data=np.ma.array(np.zeros(C.shape),
1981mask=~mask)
->1982pcolor_col=self.pcolor(coords[..., 0], coords[..., 1],
1983pcolor_data, zorder=zorder,
1984**kwargs)
1985# Now add back in the masked data if there was any1986full_mask=~maskifC_maskisNoneelse~mask|C_maskFile~/miniconda3/envs/AQUATIC/lib/python3.10/site-packages/cartopy/mpl/geoaxes.py:318, in_add_transform.<locals>.wrapper(self, *args, **kwargs)
313raiseValueError(f'Invalid transform: Spherical {func.__name__} '314'is not supported - consider using '315'PlateCarree/RotatedPole.')
317kwargs['transform'] =transform-->318returnfunc(self, *args, **kwargs)
File~/miniconda3/envs/AQUATIC/lib/python3.10/site-packages/cartopy/mpl/geoaxes.py:2027, inGeoAxes.pcolor(self, *args, **kwargs)
2024result=matplotlib.axes.Axes.pcolor(self, *args, **kwargs)
2026# Update the datalim for this pcolor.->2027limits=result.get_datalim(self.transData)
2028self.update_datalim(limits)
2030self.autoscale_view()
File~/miniconda3/envs/AQUATIC/lib/python3.10/site-packages/matplotlib/collections.py:280, inCollection.get_datalim(self, transData)
277returntransforms.Bbox.null()
279ifnottransform.is_affine:
-->280paths= [transform.transform_path_non_affine(p) forpinpaths]
281# Don't convert transform to transform.get_affine() here because282# we may have transform.contains_branch(transData) but not283# transforms.get_affine().contains_branch(transData). But later,284# be careful to only apply the affine part that remains.286ifany(transform.contains_branch_seperately(transData)):
File~/miniconda3/envs/AQUATIC/lib/python3.10/site-packages/matplotlib/collections.py:280, in<listcomp>(.0)
277returntransforms.Bbox.null()
279ifnottransform.is_affine:
-->280paths= [transform.transform_path_non_affine(p) forpinpaths]
281# Don't convert transform to transform.get_affine() here because282# we may have transform.contains_branch(transData) but not283# transforms.get_affine().contains_branch(transData). But later,284# be careful to only apply the affine part that remains.286ifany(transform.contains_branch_seperately(transData)):
File~/miniconda3/envs/AQUATIC/lib/python3.10/site-packages/matplotlib/transforms.py:2436, inCompositeGenericTransform.transform_path_non_affine(self, path)
2434returnpath2435elifnotself._a.is_affineandself._b.is_affine:
->2436returnself._a.transform_path_non_affine(path)
2437else:
2438returnself._b.transform_path_non_affine(
2439self._a.transform_path(path))
File~/miniconda3/envs/AQUATIC/lib/python3.10/site-packages/cartopy/mpl/geoaxes.py:190, inInterProjectionTransform.transform_path_non_affine(self, src_path)
186geoms=cpatch.path_to_geos(src_path,
187getattr(self, 'force_path_ccw', False))
189forgeomingeoms:
-->190proj_geom=self.target_projection.project_geometry(
191geom, self.source_projection)
192transformed_geoms.append(proj_geom)
194ifnottransformed_geoms:
File~/miniconda3/envs/AQUATIC/lib/python3.10/site-packages/cartopy/crs.py:805, inProjection.project_geometry(self, geometry, src_crs)
803ifnotmethod_name:
804raiseValueError(f'Unsupported geometry type {geom_type!r}')
-->805returngetattr(self, method_name)(geometry, src_crs)
File~/miniconda3/envs/AQUATIC/lib/python3.10/site-packages/cartopy/crs.py:948, inProjection._project_polygon(self, polygon, src_crs)
946multi_lines= []
947forsrc_ringin [polygon.exterior] +list(polygon.interiors):
-->948p_rings, p_mline=self._project_linear_ring(src_ring, src_crs)
949ifp_rings:
950rings.extend(p_rings)
File~/miniconda3/envs/AQUATIC/lib/python3.10/site-packages/cartopy/crs.py:824, inProjection._project_linear_ring(self, linear_ring, src_crs)
819debug=False820# 1) Resolve the initial lines into projected segments821# 1abc822# def23ghi823# jkl41-->824multi_line_string=cartopy.trace.project_linear(linear_ring,
825src_crs, self)
827# Threshold for whether a point is close enough to be the same828# point as another.829threshold=max(np.abs(self.x_limits+self.y_limits)) *1e-5Filelib/cartopy/trace.pyx:628, incartopy.trace.project_linear()
Filelib/cartopy/trace.pyx:100, incartopy.trace.geos_from_shapely()
TypeError: anintegerisrequiredExceptioninTkintercallbackTraceback (mostrecentcalllast):
File"/home/andrew/miniconda3/envs/AQUATIC/lib/python3.10/tkinter/__init__.py", line1921, in__call__returnself.func(*args)
File"/home/andrew/miniconda3/envs/AQUATIC/lib/python3.10/tkinter/__init__.py", line839, incallitfunc(*args)
File"/home/andrew/miniconda3/envs/AQUATIC/lib/python3.10/site-packages/matplotlib/backends/_backend_tk.py", line252, inidle_drawself.draw()
File"/home/andrew/miniconda3/envs/AQUATIC/lib/python3.10/site-packages/matplotlib/backends/backend_tkagg.py", line9, indrawsuper().draw()
File"/home/andrew/miniconda3/envs/AQUATIC/lib/python3.10/site-packages/matplotlib/backends/backend_agg.py", line436, indrawself.figure.draw(self.renderer)
File"/home/andrew/miniconda3/envs/AQUATIC/lib/python3.10/site-packages/matplotlib/artist.py", line74, indraw_wrapperresult=draw(artist, renderer, *args, **kwargs)
File"/home/andrew/miniconda3/envs/AQUATIC/lib/python3.10/site-packages/matplotlib/artist.py", line51, indraw_wrapperreturndraw(artist, renderer)
File"/home/andrew/miniconda3/envs/AQUATIC/lib/python3.10/site-packages/matplotlib/figure.py", line2845, indrawmimage._draw_list_compositing_images(
File"/home/andrew/miniconda3/envs/AQUATIC/lib/python3.10/site-packages/matplotlib/image.py", line132, in_draw_list_compositing_imagesa.draw(renderer)
File"/home/andrew/miniconda3/envs/AQUATIC/lib/python3.10/site-packages/matplotlib/artist.py", line51, indraw_wrapperreturndraw(artist, renderer)
File"/home/andrew/miniconda3/envs/AQUATIC/lib/python3.10/site-packages/cartopy/mpl/geoaxes.py", line558, indrawreturnmatplotlib.axes.Axes.draw(self, renderer=renderer, **kwargs)
File"/home/andrew/miniconda3/envs/AQUATIC/lib/python3.10/site-packages/matplotlib/artist.py", line51, indraw_wrapperreturndraw(artist, renderer)
File"/home/andrew/miniconda3/envs/AQUATIC/lib/python3.10/site-packages/matplotlib/axes/_base.py", line3091, indrawmimage._draw_list_compositing_images(
File"/home/andrew/miniconda3/envs/AQUATIC/lib/python3.10/site-packages/matplotlib/image.py", line132, in_draw_list_compositing_imagesa.draw(renderer)
File"/home/andrew/miniconda3/envs/AQUATIC/lib/python3.10/site-packages/matplotlib/artist.py", line51, indraw_wrapperreturndraw(artist, renderer)
File"/home/andrew/miniconda3/envs/AQUATIC/lib/python3.10/site-packages/matplotlib/collections.py", line990, indrawsuper().draw(renderer)
File"/home/andrew/miniconda3/envs/AQUATIC/lib/python3.10/site-packages/matplotlib/artist.py", line51, indraw_wrapperreturndraw(artist, renderer)
File"/home/andrew/miniconda3/envs/AQUATIC/lib/python3.10/site-packages/matplotlib/collections.py", line366, indrawtransform, transOffset, offsets, paths=self._prepare_points()
File"/home/andrew/miniconda3/envs/AQUATIC/lib/python3.10/site-packages/matplotlib/collections.py", line343, in_prepare_pointspaths= [transform.transform_path_non_affine(path)
File"/home/andrew/miniconda3/envs/AQUATIC/lib/python3.10/site-packages/matplotlib/collections.py", line343, in<listcomp>paths= [transform.transform_path_non_affine(path)
File"/home/andrew/miniconda3/envs/AQUATIC/lib/python3.10/site-packages/matplotlib/transforms.py", line2436, intransform_path_non_affinereturnself._a.transform_path_non_affine(path)
File"/home/andrew/miniconda3/envs/AQUATIC/lib/python3.10/site-packages/cartopy/mpl/geoaxes.py", line190, intransform_path_non_affineproj_geom=self.target_projection.project_geometry(
File"/home/andrew/miniconda3/envs/AQUATIC/lib/python3.10/site-packages/cartopy/crs.py", line805, inproject_geometryreturngetattr(self, method_name)(geometry, src_crs)
File"/home/andrew/miniconda3/envs/AQUATIC/lib/python3.10/site-packages/cartopy/crs.py", line948, in_project_polygonp_rings, p_mline=self._project_linear_ring(src_ring, src_crs)
File"/home/andrew/miniconda3/envs/AQUATIC/lib/python3.10/site-packages/cartopy/crs.py", line824, in_project_linear_ringmulti_line_string=cartopy.trace.project_linear(linear_ring,
File"lib/cartopy/trace.pyx", line628, incartopy.trace.project_linearFile"lib/cartopy/trace.pyx", line100, incartopy.trace.geos_from_shapelyTypeError: anintegerisrequiredExceptioninTkintercallbackTraceback (mostrecentcalllast):
File"/home/andrew/miniconda3/envs/AQUATIC/lib/python3.10/tkinter/__init__.py", line1921, in__call__returnself.func(*args)
File"/home/andrew/miniconda3/envs/AQUATIC/lib/python3.10/tkinter/__init__.py", line839, incallitfunc(*args)
File"/home/andrew/miniconda3/envs/AQUATIC/lib/python3.10/site-packages/matplotlib/backends/_backend_tk.py", line252, inidle_drawself.draw()
File"/home/andrew/miniconda3/envs/AQUATIC/lib/python3.10/site-packages/matplotlib/backends/backend_tkagg.py", line9, indrawsuper().draw()
File"/home/andrew/miniconda3/envs/AQUATIC/lib/python3.10/site-packages/matplotlib/backends/backend_agg.py", line436, indrawself.figure.draw(self.renderer)
File"/home/andrew/miniconda3/envs/AQUATIC/lib/python3.10/site-packages/matplotlib/artist.py", line74, indraw_wrapperresult=draw(artist, renderer, *args, **kwargs)
File"/home/andrew/miniconda3/envs/AQUATIC/lib/python3.10/site-packages/matplotlib/artist.py", line51, indraw_wrapperreturndraw(artist, renderer)
File"/home/andrew/miniconda3/envs/AQUATIC/lib/python3.10/site-packages/matplotlib/figure.py", line2845, indrawmimage._draw_list_compositing_images(
File"/home/andrew/miniconda3/envs/AQUATIC/lib/python3.10/site-packages/matplotlib/image.py", line132, in_draw_list_compositing_imagesa.draw(renderer)
File"/home/andrew/miniconda3/envs/AQUATIC/lib/python3.10/site-packages/matplotlib/artist.py", line51, indraw_wrapperreturndraw(artist, renderer)
File"/home/andrew/miniconda3/envs/AQUATIC/lib/python3.10/site-packages/cartopy/mpl/geoaxes.py", line558, indrawreturnmatplotlib.axes.Axes.draw(self, renderer=renderer, **kwargs)
File"/home/andrew/miniconda3/envs/AQUATIC/lib/python3.10/site-packages/matplotlib/artist.py", line51, indraw_wrapperreturndraw(artist, renderer)
File"/home/andrew/miniconda3/envs/AQUATIC/lib/python3.10/site-packages/matplotlib/axes/_base.py", line3091, indrawmimage._draw_list_compositing_images(
File"/home/andrew/miniconda3/envs/AQUATIC/lib/python3.10/site-packages/matplotlib/image.py", line132, in_draw_list_compositing_imagesa.draw(renderer)
File"/home/andrew/miniconda3/envs/AQUATIC/lib/python3.10/site-packages/matplotlib/artist.py", line51, indraw_wrapperreturndraw(artist, renderer)
File"/home/andrew/miniconda3/envs/AQUATIC/lib/python3.10/site-packages/matplotlib/collections.py", line990, indrawsuper().draw(renderer)
File"/home/andrew/miniconda3/envs/AQUATIC/lib/python3.10/site-packages/matplotlib/artist.py", line51, indraw_wrapperreturndraw(artist, renderer)
File"/home/andrew/miniconda3/envs/AQUATIC/lib/python3.10/site-packages/matplotlib/collections.py", line366, indrawtransform, transOffset, offsets, paths=self._prepare_points()
File"/home/andrew/miniconda3/envs/AQUATIC/lib/python3.10/site-packages/matplotlib/collections.py", line343, in_prepare_pointspaths= [transform.transform_path_non_affine(path)
File"/home/andrew/miniconda3/envs/AQUATIC/lib/python3.10/site-packages/matplotlib/collections.py", line343, in<listcomp>paths= [transform.transform_path_non_affine(path)
File"/home/andrew/miniconda3/envs/AQUATIC/lib/python3.10/site-packages/matplotlib/transforms.py", line2436, intransform_path_non_affinereturnself._a.transform_path_non_affine(path)
File"/home/andrew/miniconda3/envs/AQUATIC/lib/python3.10/site-packages/cartopy/mpl/geoaxes.py", line190, intransform_path_non_affineproj_geom=self.target_projection.project_geometry(
File"/home/andrew/miniconda3/envs/AQUATIC/lib/python3.10/site-packages/cartopy/crs.py", line805, inproject_geometryreturngetattr(self, method_name)(geometry, src_crs)
File"/home/andrew/miniconda3/envs/AQUATIC/lib/python3.10/site-packages/cartopy/crs.py", line948, in_project_polygonp_rings, p_mline=self._project_linear_ring(src_ring, src_crs)
File"/home/andrew/miniconda3/envs/AQUATIC/lib/python3.10/site-packages/cartopy/crs.py", line824, in_project_linear_ringmulti_line_string=cartopy.trace.project_linear(linear_ring,
File"lib/cartopy/trace.pyx", line628, incartopy.trace.project_linearFile"lib/cartopy/trace.pyx", line100, incartopy.trace.geos_from_shapelyTypeError: anintegerisrequired
Anything else we need to know?
No response
Environment
/home/andrew/miniconda3/envs/AQUATIC/lib/python3.10/site-packages/_distutils_hack/init.py:33: UserWarning: Setuptools is replacing distutils.
warnings.warn("Setuptools is replacing distutils.")
INSTALLED VERSIONS
commit: None
python: 3.10.5 | packaged by conda-forge | (main, Jun 14 2022, 07:04:59) [GCC 10.3.0]
python-bits: 64
OS: Linux
OS-release: 5.4.0-122-generic
machine: x86_64
processor: x86_64
byteorder: little
LC_ALL: None
LANG: en_US.UTF-8
LOCALE: ('en_US', 'UTF-8')
libhdf5: 1.12.2
libnetcdf: 4.8.1
this seems to be a compatibility issue between cartopy and shapely=1.8.3, see SciTools/cartopy#2067. Installing shapely!=1.8.3 should avoid the TypeError for now.
closing since we can't really do anything about this.
What happened?
I am encountering an error while attempting to plot xarray fields using cartopy & matplotlib. [I'm posting here first but I'm not sure if the error is due to xarray, cartopy, or matplotlib].
What did you expect to happen?
Here is an example of the error using a plot from @rabernat's course: https://earth-env-data-science.github.io/lectures/mapping_cartopy.html#xarray-integration
Minimal Complete Verifiable Example
MVCE confirmation
Relevant log output
Anything else we need to know?
No response
Environment
/home/andrew/miniconda3/envs/AQUATIC/lib/python3.10/site-packages/_distutils_hack/init.py:33: UserWarning: Setuptools is replacing distutils.
warnings.warn("Setuptools is replacing distutils.")
INSTALLED VERSIONS
commit: None
python: 3.10.5 | packaged by conda-forge | (main, Jun 14 2022, 07:04:59) [GCC 10.3.0]
python-bits: 64
OS: Linux
OS-release: 5.4.0-122-generic
machine: x86_64
processor: x86_64
byteorder: little
LC_ALL: None
LANG: en_US.UTF-8
LOCALE: ('en_US', 'UTF-8')
libhdf5: 1.12.2
libnetcdf: 4.8.1
xarray: 2022.6.0
pandas: 1.4.3
numpy: 1.23.2
scipy: 1.9.0
netCDF4: 1.6.0
pydap: None
h5netcdf: None
h5py: None
Nio: None
zarr: None
cftime: 1.6.1
nc_time_axis: None
PseudoNetCDF: None
rasterio: None
cfgrib: None
iris: None
bottleneck: None
dask: 2022.8.0
distributed: 2022.8.0
matplotlib: 3.5.3
cartopy: 0.20.3
seaborn: 0.11.2
numbagg: None
fsspec: 2022.7.1
cupy: None
pint: None
sparse: None
flox: None
numpy_groupies: None
setuptools: 65.0.2
pip: 22.2.2
conda: None
pytest: None
IPython: 8.4.0
sphinx: None
The text was updated successfully, but these errors were encountered: