1+ import matplotlib .pyplot as plt
2+ from mpl_toolkits .basemap import Basemap
3+ import numpy as np
4+
5+ # 画海岸线
6+ def drawcoast ():
7+
8+ plt .figure (figsize = (12 , 8 ))
9+ m = Basemap () # 创建一个地图
10+ m .drawcoastlines () # 画海岸线
11+ plt .show () # 显示图像
12+
13+ # 地球基本画法
14+ def draw_basic ():
15+ map = Basemap (projection = 'ortho' , lat_0 = 0 , lon_0 = 0 )
16+ map .drawmapboundary (fill_color = 'aqua' )
17+ map .fillcontinents (color = 'gray' ,lake_color = 'aqua' )
18+ map .drawcoastlines ()
19+ plt .show ()
20+
21+ # 画中国地图
22+ def draw_china ():
23+ plt .figure (figsize = (10 , 6 ))
24+ m = Basemap (llcrnrlon = 77 , llcrnrlat = 14 , urcrnrlon = 140 , urcrnrlat = 51 , projection = 'lcc' , lat_1 = 33 , lat_2 = 45 ,
25+ lon_0 = 100 )
26+ m .drawcountries (linewidth = 1.5 )
27+ m .drawcoastlines ()
28+ plt .show ()
29+
30+ # 画地球人口分布图
31+ def drawearth ():
32+ names = []
33+ pops = []
34+ lats = []
35+ lons = []
36+ countries = []
37+ file = open ("data/main_city" , encoding = 'utf-8' ).readlines ()
38+ for line in file :
39+ info = line .split ()
40+ names .append (info [0 ])
41+ pops .append (float (info [1 ]))
42+ lat = float (info [2 ][:- 1 ])
43+ if info [2 ][- 1 ] == 'S' : lat = - lat
44+ lats .append (lat )
45+ lon = float (info [3 ][:- 1 ])
46+ if info [3 ][- 1 ] == 'W' : lon = - lon + 360.0
47+ lons .append (lon )
48+ country = info [4 ]
49+ countries .append (country )
50+ # set up map projection with
51+ # use low resolution coastlines.
52+ map = Basemap (projection = 'ortho' , lat_0 = 35 , lon_0 = 120 , resolution = 'l' )
53+ # draw coastlines, country boundaries, fill continents.
54+ map .drawcoastlines (linewidth = 0.25 )
55+ map .drawcountries (linewidth = 0.25 )
56+ # draw the edge of the map projection region (the projection limb)
57+ map .drawmapboundary (fill_color = '#689CD2' )
58+ # draw lat/lon grid lines every 30 degrees.
59+ map .drawmeridians (np .arange (0 , 360 , 30 ))
60+ map .drawparallels (np .arange (- 90 , 90 , 30 ))
61+ # Fill continent wit a different color
62+ map .fillcontinents (color = '#BF9E30' , lake_color = '#689CD2' , zorder = 0 )
63+ # compute native map projection coordinates of lat/lon grid.
64+ x , y = map (lons , lats )
65+ max_pop = max (pops )
66+ # Plot each city in a loop.
67+ # Set some parameters
68+ size_factor = 80.0
69+ y_offset = 15.0
70+ rotation = 30
71+ for i , j , k , name in zip (x , y , pops , names ):
72+ size = size_factor * k / max_pop
73+ cs = map .scatter (i , j , s = size , marker = 'o' , color = '#FF5600' )
74+ plt .text (i , j + y_offset , name , rotation = rotation , fontsize = 10 )
75+
76+ plt .title ('earth' )
77+ plt .show ()
78+
79+ # 画带投影的地球图片
80+ def draw_earth1 ():
81+ import matplotlib .pyplot as plt
82+ from mpl_toolkits .basemap import Basemap
83+ plt .figure (figsize = (8 , 8 ))
84+ # 正射投影,投影原点设在了上海周边
85+ m = Basemap (projection = 'ortho' , resolution = None , lat_0 = 30 , lon_0 = 120 )
86+ # 图像原始分辨率是5400*2700,设置scale = 0.5以后分辨率为2700*1350,如此作图
87+ # 迅速不少也不那么占用内存了
88+ m .bluemarble (scale = 0.5 )
89+ plt .show ()
90+
91+ if __name__ == '__main__' :
92+ #drawcoast()
93+ drawearth ()
94+ #draw_basic()
95+ #draw_china()
96+ #draw_earth1()
0 commit comments