我可以在 Python 中使用 matplotlib 的 Basemap 来绘制地球仪,我可以设置地球仪的颜色以及我在其上绘制的任何内容(大陆等) ).但是这个地球图像设置在白色背景上;如何更改该白色背景的颜色?
代码如下:
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')
产生这个,我想改变白色背景
请您参考如下方法:
您可以使用以下方法执行此操作:
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")
当您使用“plt.show()”显示它时,这会创建一个蓝色背景图,但保存的 map 图像具有红色背景。这样做的原因是因为您使用不同的功能将图像渲染到不同的设备。可以找到更多详细信息here和 here .希望这可以帮助。
