Matplotlib底图:离线地图

时间:2018-10-11 13:55:41

标签: python matplotlib matplotlib-basemap

您好,当我以最大分辨率(resolution ='f')使用底图时,需要花费很多时间来绘制它。

对于我的项目,我必须使用此分辨率,但是由于该程序必须由用户使用,因此我希望它尽快。

是因为它是从某个在线存储库下载地图而发生的吗?

是否可以下载所需的地图并离线绘制?

这是MWE:

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

lon_min = -10.5
lon_max = 2.0
lat_min = 50.0
lat_max = 59.7

coastlinesMap = Basemap(suppress_ticks=False, projection='cyl',
                        llcrnrlat=lat_min, urcrnrlat=lat_max,
                        llcrnrlon=lon_min, urcrnrlon=lon_max,
                        resolution='f')

n = 100
x = np.linspace(lon_min, lon_max, n)
y = np.linspace(lat_min, lat_max, n)
lon, lat = np.meshgrid(x, y)
z = np.zeros((n, n))
for i in range(n):
    for j in range(n):
        z[i, j] = x[i] + y[j]

fig = plt.figure("test")
ax = fig.add_subplot(111)
cf = ax.contourf(lon, lat, z, cmap="jet")
ax.set_xlabel("lon")
ax.set_ylabel("lat")
ax.add_collection(coastlinesMap.drawcoastlines(linewidth=0.25))
coastlinesMap.fillcontinents(color="green")
fig.colorbar(cf)

plt.show()

修改

我只想澄清一下,当显示该图时,我可以缩放或平移等等,并且更改很快,而生成coastlineMap的时候就很慢。

1 个答案:

答案 0 :(得分:0)

按照@ImportanceOfBeingErnest的链接,环顾四周,我使用cPickle解决了我的问题。

我在下面报告解决方案

import numpy as np
import matplotlib.pyplot as plt
from mpl_toolkits.basemap import Basemap
import time
from six.moves import cPickle

lon_min = -10.5
lon_max = 2.0
lat_min = 50.0
lat_max = 59.7

t1 = time.clock()
coastlinesMap = Basemap(suppress_ticks=False, projection='cyl',
                        llcrnrlat=lat_min, urcrnrlat=lat_max,
                        llcrnrlon=lon_min, urcrnrlon=lon_max,
                        resolution='f')
print(time.clock()-t1,' secs to create original Basemap instance')
cPickle.dump(coastlinesMap, open('UKmap.pickle', 'wb'), -1)

n = 100
x = np.linspace(lon_min, lon_max, n)
y = np.linspace(lat_min, lat_max, n)
lon, lat = np.meshgrid(x, y)
z = np.zeros((n, n))
for i in range(n):
    for j in range(n):
        z[i, j] = x[i] + y[j]

fig = plt.figure("test")
ax = fig.add_subplot(111)
cf = ax.contourf(lon, lat, z, cmap="jet")
ax.set_xlabel("lon")
ax.set_ylabel("lat")
ax.add_collection(coastlinesMap.drawcoastlines(linewidth=0.25))
coastlinesMap.fillcontinents(color="green")
fig.colorbar(cf)

t1 = time.clock()
m2 = cPickle.load(open('UKmap.pickle','rb'))

fig = plt.figure("test")
ax = fig.add_subplot(111)
ax.set_xlabel("lon")
ax.set_ylabel("lat")
ax.add_collection(m2.drawcoastlines(linewidth=0.25))
m2.drawcountries()
print(time.clock()-t1,' secs to plot using using a pickled Basemap instance')

plt.show()