使用matplotlib-basemap为多个绘图使用一个背景地图?

时间:2017-09-21 13:16:36

标签: python matplotlib plot maps matplotlib-basemap

Matplotlibs底图模块具有为inbuilt functions for some basic maps绘制地图背景的能力以及与arcgis maps绑定的能力。

两个选项都运行良好,但速度很慢(see also here),尤其是arcgis选项。

对于单个情节,这不是一个大问题,但是有多个情节(在我的情况下大约500)这变得非常等待,此外,我不想向arcgis发送数百个请求,无论如何都要获取相同的地图。

但是如何设置/下载背景地图一次,并在绘制其中许多版本的循环中继续使用它?

我已将the great circle example from the documentation与etopo背景相匹配,并通过一些颜色循环进行测试:

from mpl_toolkits.basemap import Basemap
import numpy as np
import matplotlib.pyplot as plt
# create new figure, axes instances.
fig=plt.figure()
ax=fig.add_axes([0.1,0.1,0.8,0.8])
# setup mercator map projection.
m = Basemap(llcrnrlon=-100.,llcrnrlat=20.,urcrnrlon=20.,urcrnrlat=60.,\
            rsphere=(6378137.00,6356752.3142),\
            resolution='l',projection='merc',\
            lat_0=40.,lon_0=-20.,lat_ts=20.)
# nylat, nylon are lat/lon of New York
nylat = 40.78; nylon = -73.98
# lonlat, lonlon are lat/lon of London.
lonlat = 51.53; lonlon = 0.08
# draw great circle route between NY and London
m.drawcoastlines()
m.etopo()
# draw parallels
m.drawparallels(np.arange(10,90,20),labels=[1,1,0,1])
# draw meridians
m.drawmeridians(np.arange(-180,180,30),labels=[1,1,0,1])

colors = ('r','b','k','y')
for p_color in colors:
    m.drawgreatcircle(nylon,nylat,lonlon,lonlat,linewidth=2,color=p_color)
    filename = '%s.png' % p_color
    plt.savefig(filename, dpi=100)

上述作品,但仅仅因为我没有关闭情节。凭借我真实的数据,我会很快耗尽记忆力。如果我在循环结束时添加plt.close(),我会丢失背景地图。当我将绘图的设置放入循环时也一样:

from mpl_toolkits.basemap import Basemap
import numpy as np
import matplotlib.pyplot as plt
# create new figure, axes instances.
# setup mercator map projection.
m = Basemap(llcrnrlon=-100.,llcrnrlat=20.,urcrnrlon=20.,urcrnrlat=60.,\
            rsphere=(6378137.00,6356752.3142),\
            resolution='l',projection='merc',\
            lat_0=40.,lon_0=-20.,lat_ts=20.)
# nylat, nylon are lat/lon of New York
nylat = 40.78; nylon = -73.98
# lonlat, lonlon are lat/lon of London.
lonlat = 51.53; lonlon = 0.08
# draw great circle route between NY and London
m.drawcoastlines()
m.etopo()
# draw parallels
m.drawparallels(np.arange(10,90,20),labels=[1,1,0,1])
# draw meridians
m.drawmeridians(np.arange(-180,180,30),labels=[1,1,0,1])

colors = ('r','b','k','y')
for p_color in colors:
    fig=plt.figure()
    ax=fig.add_axes([0.1,0.1,0.8,0.8])
    m.drawgreatcircle(nylon,nylat,lonlon,lonlat,linewidth=2,color=p_color)
    filename = '%s.png' % p_color
    plt.savefig(filename, dpi=100)
    plt.close()

当我将整个m.Basemap…m.etopo内容放入循环中时,它似乎只能工作(忽略第一个选项及其内存问题),但这太慢了。

如何设置'm'并继续使用?

我可能通过绘制背景地图一次,将我的数据绘制到透明背景上,然后将其与imagemagick结合起来,然后我觉得应该有一个更好的解决方案。

1 个答案:

答案 0 :(得分:1)

我想这个想法是保留底图,而不是改变它。在循环中添加您想要从图像更改为图像的艺术家,保存图形,然后删除艺术家。

from mpl_toolkits.basemap import Basemap
import numpy as np
import matplotlib.pyplot as plt
# create new figure, axes instances.
# setup mercator map projection.
fig=plt.figure()
ax=fig.add_axes([0.1,0.1,0.8,0.8])
m = Basemap(llcrnrlon=-100.,llcrnrlat=20.,urcrnrlon=20.,urcrnrlat=60.,\
            rsphere=(6378137.00,6356752.3142),\
            resolution='l',projection='merc',\
            lat_0=40.,lon_0=-20.,lat_ts=20.)
# nylat, nylon are lat/lon of New York
nylat = 40.78; nylon = -73.98
# lonlat, lonlon are lat/lon of London.
lonlat = 51.53; lonlon = 0.08
# draw great circle route between NY and London
m.drawcoastlines()
m.etopo()
# draw parallels
m.drawparallels(np.arange(10,90,20),labels=[1,1,0,1])
# draw meridians
m.drawmeridians(np.arange(-180,180,30),labels=[1,1,0,1])

colors = ('r','b','k','y')
for p_color in colors:
    gc = m.drawgreatcircle(nylon,nylat,lonlon,lonlat,linewidth=2,color=p_color)
    filename = 'output{}.png'.format(p_color)
    plt.savefig(filename, dpi=100)
    # remove previous plot from axes
    for line in gc:
        line.remove()
    del gc