使用 matplotlib-basemap 将一张背景图用于多个地块?
Using one background map for multiple plots with matplotlib-basemap?
Matplotlibs basemap 模块具有绘制地图背景的能力,inbuilt functions for some basic maps and the ability to tie into arcgis maps.
这两个选项都很好,但确实很慢 (see also here),尤其是 arcgis 选项。
对于单个地块,这不是什么大问题,但是对于多个地块(在我的情况下大约 500 个),这就变成了等待练习,另外,我不想向 arcgis 发送数百个请求,无论如何总是获取相同的地图。
但是如何设置 up/download 一次背景图,并在绘制多个版本的循环中继续使用它?
我已将 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 结合起来,来实现一些 MacGyver 的东西,但我觉得应该有更好的解决方案。
我想这个想法是保留底图图而不更改它。在循环中添加您想要从图像更改为图像的艺术家,保存图形,然后删除艺术家。
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
Matplotlibs basemap 模块具有绘制地图背景的能力,inbuilt functions for some basic maps and the ability to tie into arcgis maps.
这两个选项都很好,但确实很慢 (see also here),尤其是 arcgis 选项。
对于单个地块,这不是什么大问题,但是对于多个地块(在我的情况下大约 500 个),这就变成了等待练习,另外,我不想向 arcgis 发送数百个请求,无论如何总是获取相同的地图。
但是如何设置 up/download 一次背景图,并在绘制多个版本的循环中继续使用它?
我已将 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 结合起来,来实现一些 MacGyver 的东西,但我觉得应该有更好的解决方案。
我想这个想法是保留底图图而不更改它。在循环中添加您想要从图像更改为图像的艺术家,保存图形,然后删除艺术家。
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