ERROR with Cartopy in python 3: "AttributeError: '_Shape' object has no attribute 'get'

Viewed 547

I am running Python 3.9 on Linux along with Cartopy 0.18.0 and matplotlib 3.4.2. I have just started experiencing an error with making maps with Cartopy. The following occurs when either I show the figure (plt.show()) or save a figure (plt.savefig()):

import os,sys,cartopy,warnings
import numpy as np
import cartopy.crs as ccrs
import matplotlib.pyplot as plt
from cartopy.util import add_cyclic_point

# Load data, perform calculations...
# .....
# .....
# .....


# Make map of results
cmin = -0.5; cmax = 0.51; cint = 0.05
clevs = np.round(np.arange(cmin,cmax,cint),2)
nlevs = len(clevs) - 1

cmap = plt.get_cmap(name='RdBu_r',lut=nlevs)

states_provinces = cartopy.feature.NaturalEarthFeature(
                   category='cultural',
                   name='admin_1_states_provinces_lines',
                   scale='50m',
                   facecolor='none')

projection = ccrs.Miller(central_longitude=-180.)
data_crs = ccrs.PlateCarree()
extent = [-270,-30,20,80] # Set the map extents

plot_data,plot_lon = add_cyclic_point(data,coord=lon,axis=0)
lat,lon = np.meshgrid(lat,plot_lon)

plt.figure()
ax = plt.subplot(111,projection=projection)
ax.set_extent(extent,crs=data_crs)
ax.add_feature(cartopy.feature.BORDERS,edgecolor='gray',linewidth=1)
ax.add_feature(states_provinces,edgecolor='gray',linewidth=1)
    ax.add_feature(cartopy.feature.LAKES,facecolor='white',edgecolor='gray',linewidth=1)
ax.coastlines(resolution='50m',color='black',linewidth=1)
cs=ax.contourf(lon,lat,plot_data,clevs,cmap=cmap,transform=data_crs)
    
cbar=plt.colorbar(cs,orientation='horizontal')

cticks = []
for i in clevs:
    cticks.append(int(i)) if i.is_integer() else cticks.append(i)

cbar.set_ticks(clevs[::2])
cbar.set_ticklabels(cticks[::2])

for i in cbar.ax.yaxis.get_ticklabels():
    i.set_size(14)

plt.title('Test Plot',size=16)
    
# plt.show(block=False)
plt.savefig('Test.png',dpi=400)

I receive the following error:

  File "/share/apps/anaconda2/envs/py39/lib/python3.9/site-packages/matplotlib/pyplot.py", line 966, in savefig
    res = fig.savefig(*args, **kwargs)
  File "/share/apps/anaconda2/envs/py39/lib/python3.9/site-packages/matplotlib/figure.py", line 3005, in savefig
    self.canvas.print_figure(fname, **kwargs)
  File "/share/apps/anaconda2/envs/py39/lib/python3.9/site-packages/matplotlib/backend_bases.py", line 2255, in print_figure
    result = print_method(
  File "/share/apps/anaconda2/envs/py39/lib/python3.9/site-packages/matplotlib/backend_bases.py", line 1669, in wrapper
    return func(*args, **kwargs)
  File "/share/apps/anaconda2/envs/py39/lib/python3.9/site-packages/matplotlib/backends/backend_agg.py", line 508, in print_png
    FigureCanvasAgg.draw(self)
  File "/share/apps/anaconda2/envs/py39/lib/python3.9/site-packages/matplotlib/backends/backend_agg.py", line 406, in draw
    self.figure.draw(self.renderer)
  File "/share/apps/anaconda2/envs/py39/lib/python3.9/site-packages/matplotlib/artist.py", line 74, in draw_wrapper
    result = draw(artist, renderer, *args, **kwargs)
  File "/share/apps/anaconda2/envs/py39/lib/python3.9/site-packages/matplotlib/artist.py", line 51, in draw_wrapper
    return draw(artist, renderer, *args, **kwargs)
  File "/share/apps/anaconda2/envs/py39/lib/python3.9/site-packages/matplotlib/figure.py", line 2780, in draw
    mimage._draw_list_compositing_images(
  File "/share/apps/anaconda2/envs/py39/lib/python3.9/site-packages/matplotlib/image.py", line 132, in _draw_list_compositing_images
    a.draw(renderer)
  File "/share/apps/anaconda2/envs/py39/lib/python3.9/site-packages/matplotlib/artist.py", line 51, in draw_wrapper
    return draw(artist, renderer, *args, **kwargs)
  File "/share/apps/anaconda2/envs/py39/lib/python3.9/site-packages/cartopy/mpl/geoaxes.py", line 480, in draw
    return matplotlib.axes.Axes.draw(self, renderer=renderer, **kwargs)
  File "/share/apps/anaconda2/envs/py39/lib/python3.9/site-packages/matplotlib/artist.py", line 51, in draw_wrapper
    return draw(artist, renderer, *args, **kwargs)
  File "/share/apps/anaconda2/envs/py39/lib/python3.9/site-packages/matplotlib/_api/deprecation.py", line 431, in wrapper
    return func(*inner_args, **inner_kwargs)
  File "/share/apps/anaconda2/envs/py39/lib/python3.9/site-packages/matplotlib/axes/_base.py", line 2921, in draw
    mimage._draw_list_compositing_images(renderer, self, artists)
  File "/share/apps/anaconda2/envs/py39/lib/python3.9/site-packages/matplotlib/image.py", line 132, in _draw_list_compositing_images
    a.draw(renderer)
  File "/share/apps/anaconda2/envs/py39/lib/python3.9/site-packages/matplotlib/artist.py", line 51, in draw_wrapper
    return draw(artist, renderer, *args, **kwargs)
  File "/share/apps/anaconda2/envs/py39/lib/python3.9/site-packages/cartopy/mpl/feature_artist.py", line 155, in draw
    geoms = self._feature.intersecting_geometries(extent)
  File "/share/apps/anaconda2/envs/py39/lib/python3.9/site-packages/cartopy/feature/__init__.py", line 302, in intersecting_geometries
    return super(NaturalEarthFeature, self).intersecting_geometries(extent)
  File "/share/apps/anaconda2/envs/py39/lib/python3.9/site-packages/cartopy/feature/__init__.py", line 110, in intersecting_geometries
    return (geom for geom in self.geometries() if
  File "/share/apps/anaconda2/envs/py39/lib/python3.9/site-packages/cartopy/feature/__init__.py", line 287, in geometries
    geometries = tuple(shapereader.Reader(path).geometries())
  File "/share/apps/anaconda2/envs/py39/lib/python3.9/site-packages/cartopy/io/shapereader.py", line 169, in geometries
    yield sgeom.shape(shape)
  File "/share/apps/anaconda2/envs/py39/lib/python3.9/site-packages/shapely/geometry/geo.py", line 97, in shape
    geom_type = ob.get("type").lower()
AttributeError: '_Shape' object has no attribute 'get'

If I comment out the map features in the above code (e.g., ax.add_feature()), the plot is made with the shaded contours (of course with no map image).

I have tried re-creating the conda environment, uninstalling and reinstalling cartopy, and still the error occurs. Any thoughts on this? Thank you!

0 Answers
Related