Contextily map upside down when used with cartopy

1k Views Asked by At

When a contextily basemap is added to an axis projected with cartopy, the map appears upside down. Here is a minimum example:

import matplotlib.pyplot as plt
import cartopy
import contextily

# contextily & cartopy
fig, ax = plt.subplots()
ax = plt.axes(projection=cartopy.crs.Mercator())
ax.add_feature(cartopy.feature.COASTLINE)
ax.set_extent([-11,37,35,70])
contextily.add_basemap(ax)

The following code produces a correct map, however, without cartopy features:

# contextily
fig, ax = plt.subplots()
ax.set_xlim(-1163881, 4068715)
ax.set_ylim( 4118821,11118821)
contextily.add_basemap(ax)

It looks like there is some projection issue with the cartopy coordinate system. How to add a proper basemap to a cartopy plot?

enter image description here

2

There are 2 best solutions below

1
On

I ran into this issue as well... not sure how to resolve this with contextily, but found a solution using the built in cartopy method ax.add_wmts() to access and plot basemap tiles.

url = 'http://map1c.vis.earthdata.nasa.gov/wmts-geo/wmts.cgi'
layer_name = 'BlueMarble_ShadedRelief_Bathymetry'

ax.add_wmts(url, layer_name, alpha = 0.9)

Additional layers can be found here. Just need to swap out the layer_name.

0
On

Make sure you have the latest version of contextily (1.0.0) and cartopy (0.18.0), as for me it is working fine then:

Using your exact code, with cartopy and contextily:

enter image description here

Using

>>> contextily.__version__                                                                                                                                                                                     
'1.0.0'

>>> cartopy.__version__                                                                                                                                                                                        
'0.18.0'