How to remove the frame of a matplotlib image and set background color to transparent

Viewed 734

How can i remove that black frame of the map? And is it possible to change the background color within that frame from white to transparent?

plot with black frame and white background

import cartopy.crs as ccrs
import cartopy.io.shapereader as shpreader
import matplotlib.pyplot as plt


fname = 'germany.shp'

shapes = list(shpreader.Reader(fname).geometries())

ax = plt.axes(projection=ccrs.NorthPolarStereo(central_longitude=10.0))

ax.add_geometries(shapes, ccrs.PlateCarree(), edgecolor='none', alpha=1)

ax.set_facecolor((1.0, 0.47, 0.42))

ax.set_extent([6, 15, 47, 55])

plt.savefig("map.png", format='png', dpi=400, quality=95)

plt.show()
1 Answers

Turn axis off with plt.axis('off') and save the figure transparent with plt.savefig('name.png', transparent=True).

Edit: The borders and the background that you want to change are introduced by the cartopy GeoAxes. To change them use:

ax.background_patch.set_visible(False)   # Background
ax.outline_patch.set_visible(False)      # Borders

A reference example can be found here.

Notice that the transparent=True keyword in plt.savefig is still needed.

Related