|
1 | | -from __future__ import (absolute_import, division, print_function) |
| 1 | +from __future__ import print_function |
| 2 | + |
| 3 | +import time |
| 4 | +import pickle |
2 | 5 |
|
3 | | -from mpl_toolkits.basemap import Basemap |
4 | 6 | import numpy as np |
5 | 7 | import matplotlib.pyplot as plt |
6 | | -import pickle, time |
| 8 | +from mpl_toolkits.basemap import Basemap |
| 9 | + |
7 | 10 |
|
8 | | -# create figure with aqua background (will be oceans) |
| 11 | +# Create figure. |
9 | 12 | fig = plt.figure() |
10 | 13 |
|
11 | | -# create Basemap instance. Use 'high' resolution coastlines. |
12 | | -t1 = time.clock() |
13 | | -#m = Basemap(llcrnrlon=-10.5,llcrnrlat=49.5,urcrnrlon=3.5,urcrnrlat=59.5, |
14 | | -# resolution='h',projection='tmerc',lon_0=-4,lat_0=0) |
15 | | -m = Basemap(width=920000,height=1100000, |
16 | | - resolution='f',projection='tmerc',lon_0=-4.2,lat_0=54.6) |
17 | | -# make sure countries and rivers are loaded |
18 | | -m.drawcountries() |
19 | | -m.drawrivers() |
20 | | -print(time.clock()-t1,' secs to create original Basemap instance') |
21 | | - |
22 | | -# pickle the class instance. |
23 | | -pickle.dump(m,open('map.pickle','wb'),-1) |
24 | | - |
25 | | -# clear the figure |
| 14 | +# Create Basemap instance: |
| 15 | +# - Use 'full' resolution coastlines. |
| 16 | +# - Make sure that countries and rivers are loaded. |
| 17 | +t0 = time.time() |
| 18 | +bmap1 = Basemap(width=920000, height=1100000, resolution="f", |
| 19 | + projection="tmerc", lon_0=-4.2, lat_0=54.6) |
| 20 | +bmap1.drawcountries() |
| 21 | +bmap1.drawrivers() |
| 22 | +t1 = time.time() |
| 23 | +print("{0:.3f} secs to plot with a Basemap instance created at runtime".format(t1 - t0)) |
| 24 | + |
| 25 | +# Clear the figure. |
26 | 26 | plt.clf() |
27 | | -# read pickle back in and plot it again (should be much faster). |
28 | | -t1 = time.clock() |
29 | | -m2 = pickle.load(open('map.pickle','rb')) |
30 | | -# draw coastlines and fill continents. |
31 | | -m.drawcoastlines() |
32 | | -# fill continents and lakes |
33 | | -m.fillcontinents(color='coral',lake_color='aqua') |
34 | | -# draw political boundaries. |
35 | | -m.drawcountries(linewidth=1) |
36 | | -# fill map projection region light blue (this will |
37 | | -# paint ocean areas same color as lakes). |
38 | | -m.drawmapboundary(fill_color='aqua') |
39 | | -# draw major rivers. |
40 | | -m.drawrivers(color='b') |
41 | | -print(time.clock()-t1,' secs to plot using using a pickled Basemap instance') |
42 | | -# draw parallels |
43 | | -circles = np.arange(48,65,2).tolist() |
44 | | -m.drawparallels(circles,labels=[1,1,0,0]) |
45 | | -# draw meridians |
46 | | -meridians = np.arange(-12,13,2) |
47 | | -m.drawmeridians(meridians,labels=[0,0,1,1]) |
48 | | -plt.title("High-Res British Isles",y=1.04) |
| 27 | + |
| 28 | +# Pickle the class instance. |
| 29 | +with open("map.pickle", "wb") as fd: |
| 30 | + pickle.dump(bmap1, fd, protocol=-1) |
| 31 | + |
| 32 | +# Read pickle back in and plot it again (should be much faster): |
| 33 | +# - Draw coastlines and fill continents and lakes. |
| 34 | +# - Draw political boundaries and rivers. |
| 35 | +# - Draw parallels and meridians. |
| 36 | +# - Draw map boundary and fill map background. |
| 37 | +t0 = time.time() |
| 38 | +with open("map.pickle", "rb") as fd: |
| 39 | + bmap2 = pickle.load(fd) |
| 40 | +bmap2.drawcoastlines() |
| 41 | +bmap2.fillcontinents(color="coral", lake_color="aqua") |
| 42 | +bmap2.drawcountries(linewidth=1) |
| 43 | +bmap2.drawrivers(color="b") |
| 44 | +bmap2.drawparallels(np.arange(48, 65, 2), labels=[1, 1, 0, 0]) |
| 45 | +bmap2.drawmeridians(np.arange(-12, 13, 2), labels=[0, 0, 1, 1]) |
| 46 | +bmap2.drawmapboundary(fill_color="aqua") |
| 47 | +t1 = time.time() |
| 48 | +print("{0:.3f} secs to plot with a pickled Basemap instance".format(t1 - t0)) |
| 49 | + |
| 50 | +plt.title("High-Res British Isles", y=1.04) |
49 | 51 | plt.show() |
0 commit comments