2

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

sil
  • 1,769
  • 1
  • 18
  • 34

1 Answers1

2

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

Community
  • 1
  • 1
Trond Kristiansen
  • 2,379
  • 23
  • 48
  • Good answer, but setting color to blue using `fig.patch.set_facecolor('blue')` is not necessary and introduces a bit of confusion. Or is there a reason for doing it that I missed? – snake_charmer Nov 23 '14 at 22:22
  • @snake_charmer I agree its not necessary. I just wanted to show that there are several ways of achieving the result of coloring the background. Perhaps I should remove it to avoid confusion. – Trond Kristiansen Nov 23 '14 at 22:47
  • Having both approaches is valuable, as long as they clearly appear as two distinct solutions. – snake_charmer Nov 23 '14 at 23:06