I'm trying to create a figure using Cartopy that requires a projected axis to be drawn over an unprojected axis.
Here is a simple as possible version of the code that substitutes content on the axes for background colour:
import matplotlib.pyplot as plt
import cartopy.crs as ccrs
#Setup figure
fig = plt.figure()
#Unprojected axis
ax1 = fig.add_subplot(111, axisbg='b')
#Projected axis
ax2 = fig.add_subplot(111, axisbg='None', projection=ccrs.Mercator())
plt.show()
Which instead of leaving the blue axis visible produces this:
Removing the
projection=ccrs.Mercator()
argument from the above code produces this expected result:
How do I make the projected axis background transparent?
Thanks!
Edit: I've tried these other methods of setting the background transparent with no luck:
ax2 = fig.add_subplot(111, axisbg='None', alpha=0, projection=ccrs.Mercator())
ax2.patch.set_facecolor('none')
ax2.patch.set_alpha(0)
Cartopy is still not very well-rounded in terms of controlling some things, and I'm afraid you need to dig into the code to find some things out.
I think the basic tweak you need is this :
ax2.background_patch.set_fill(False)
My hacked example :
import matplotlib.pyplot as plt
import cartopy.crs as ccrs
fig = plt.figure()
ax1 = fig.add_subplot(111, axisbg='b')
ax2 = fig.add_subplot(111, axisbg='None', projection=ccrs.Mercator())
ax2.background_patch.set_fill(False)
ax2.add_feature(cartopy.feature.LAND)
ax2.coastlines(color='red', linewidth=0.75)
plt.show()
Edit: removed copy/paste error
HTH
Patrick