sil
sil

Reputation: 2120

Changing image background colour in matplotlib Basemap ortho projection

I can use matplotlib's Basemap in Python to draw a globe, and I can set the colour of the globe and anything I draw on it (continents, etc). But this globe image is set on a white background; how do I change the colour of that white background?

Code like this:

import matplotlib
matplotlib.use('Agg')
import matplotlib.pyplot as plt
from mpl_toolkits.basemap import Basemap
m = Basemap(projection='ortho',lat_0=0,lon_0=0,resolution='c')
m.drawmapboundary(fill_color='black')
m.drawcoastlines(linewidth=1.25, color='#006600')
plt.savefig('/tmp/out.png')

produces this, with the white background that I'd like to change enter image description here

Upvotes: 2

Views: 3768

Answers (1)

Trond Kristiansen
Trond Kristiansen

Reputation: 2446

You can do this using the following:

import matplotlib
import matplotlib.pyplot as plt
from mpl_toolkits.basemap import Basemap
import matplotlib.pyplot as plt

matplotlib.use('Agg')
fig = plt.figure()
fig.patch.set_facecolor('blue')
fig.patch.set_alpha(0.7)
ax = fig.add_subplot(111)

m = Basemap(projection='ortho',lat_0=0,lon_0=0,resolution='c',ax=ax)
m.drawmapboundary(fill_color='black')
m.drawcoastlines(linewidth=1.25, color='#006600')
plt.savefig('Desktop/out.png',facecolor="red", edgecolor="blue")

This creates a plot with blue background when you show it using 'plt.show()', but the saved image of the map has a red background. The reason for this is because you render your image to different devices using different functions. More details can be found here and here. Hope this helps. T

enter image description here

Upvotes: 2

Related Questions