文章目录:
-
一.Basemap安装
-
二.地图绘制官方案
1.Basemap简介
2.案例分析
-
三.Basemap绘制地图
1.地图投影
2.绘制全球主要城市地球仪
-
四.总结
-
https://github.com/eastmountyxz/Python-zero2one
-
[Python从零到壹] 十一.数据分析之Numpy、Pandas、Matplotlib和Sklearn入门知识万字详解
-
[Python从零到壹] 十三.机器学习之聚类算法四万字总结(K-Means、BIRCH、树状聚类、MeanShift)
-
[Python从零到壹] 十八.可视化分析之Basemap地图包入门详解
作者新开的“娜璋AI安全之家”将专注于Python和安全技术,主要分享Web渗透、系统安全、人工智能、大数据分析、图像识别、恶意代码检测、CVE复现、威胁情报分析等文章。虽然作者是一名技术小白,但会保证每一篇文章都会很用心地撰写,希望这些基础性文章对你有所帮助,在Python和安全路上与大家一起进步。
pip install basemap-1.2.2-cp36-cp36m-win_amd64.whl
二.地图绘制官方案例
1.Basemap简介
2.案例分析
# -*- coding: utf-8 -*-
# By:Eastmount CSDN
from mpl_toolkits.basemap import Basemap
import matplotlib.pyplot as plt
# 设置basemap-Lambert Conformal
m = Basemap(width=12000000,height=9000000,projection='lcc',
resolution='c',lat_1=45.,lat_2=55,lat_0=50,lon_0=-107.)
# 绘制海岸线
m.drawcoastlines()
# 在地图周围绘制边界并填充背景aqua(这个背景最终成为海洋的颜色)
# 将大洲绘制在最上面
m.drawmapboundary(fill_color='aqua')
# 填充大陆coral颜色,并设置湖泊颜色为blue
m.fillcontinents(color='coral',lake_color='blue')
plt.show()
# -*- coding: utf-8 -*-
# By:Eastmount CSDN
from mpl_toolkits.basemap import Basemap
import matplotlib.pyplot as plt
# 设置basemap Lambert-Conformal
# 设置分辨率参数resolution=None 跳过处理边界数据集
m = Basemap(width=12000000,height=9000000,projection='lcc',
resolution=None,lat_1=45.,lat_2=55,lat_0=50,lon_0=-107.)
# 为地图背景绘制海陆罩
# lakes=True 意味着内陆湖和海洋颜色一致
m.drawlsmask(land_color='coral',ocean_color='aqua',lakes=True)
plt.show()
# -*- coding: utf-8 -*-
# By:Eastmount CSDN
from mpl_toolkits.basemap import Basemap
import matplotlib.pyplot as plt
m = Basemap(width=12000000,height=9000000,projection='lcc',
resolution=None,lat_1=45.,lat_2=55,lat_0=50,lon_0=-107.)
m.bluemarble()
plt.show()
# -*- coding: utf-8 -*-
# By:Eastmount CSDN
from mpl_toolkits.basemap import Basemap
import matplotlib.pyplot as plt
m = Basemap(width=12000000,height=9000000,projection='lcc',
resolution=None,lat_1=45.,lat_2=55,lat_0=50,lon_0=-107.)
m.shadedrelief()
plt.show()
# -*- coding: utf-8 -*-
# By:Eastmount CSDN
from mpl_toolkits.basemap import Basemap
import matplotlib.pyplot as plt
m = Basemap(width=12000000,height=9000000,projection='lcc',
resolution=None,lat_1=45.,lat_2=55,lat_0=50,lon_0=-107.)
m.etopo()
plt.show()
三.Basemap绘制地图
1.地图投影
m = Basemap(width=12000000,
height=9000000,
projection='lcc',
resolution=None,
lat_1=45.,
lat_2=55,
lat_0=50,
lon_0=-107.)
# -*- coding: utf-8 -*-
# By:Eastmount CSDN
from mpl_toolkits.basemap import Basemap
import numpy as np
import matplotlib.pyplot as plt
width = 28000000; lon_0 = -105; lat_0 = 40
m = Basemap(width=width,height=width,projection='aeqd',
lat_0=lat_0,lon_0=lon_0)
# 填充背景
m.drawmapboundary(fill_color='aqua')
# 绘制海岸线并填充大陆
m.drawcoastlines(linewidth=0.5)
m.fillcontinents(color='coral',lake_color='aqua')
# 20度经纬度,范围-80到81 -180到180
m.drawparallels(np.arange(-80,81,20))
m.drawmeridians(np.arange(-180,180,20))
# 在中心绘制一个黑点
xpt, ypt = m(lon_0, lat_0)
m.plot([xpt],[ypt],'ko')
# 绘制标题
plt.title('Azimuthal Equidistant Projection')
plt.show()
m = Basemap(projection='merc',
llcrnrlat=-80,
urcrnrlat=80,
llcrnrlon=-180,
urcrnrlon=180)
# -*- coding: utf-8 -*-
# By:Eastmount CSDN
from mpl_toolkits.basemap import Basemap
import matplotlib.pyplot as plt
m = Basemap(projection='merc', llcrnrlat=-80, urcrnrlat=80,
llcrnrlon=-180, urcrnrlon=180)
m.drawcoastlines()
plt.show()
# 上海经纬度 (31.23N,121.47E)
x = 31.23
y = 121.47
xpt, ypt = m(x,y)
m.scatter(xpt,ypt,s=2)
x = 31.23
y = 121.47
xpt, ypt = m(x,y)
m.plot([xpt],[ypt],'ko')
2.绘制全球主要城市地球仪
Shanghai 23019148 31.23N 121.47E China
Mumbai 12478447 18.96N 72.82E India
Karachi 13050000 24.86N 67.01E Pakistan
Delhi 16314838 28.67N 77.21E India
Manila 11855975 14.62N 120.97E Philippines
Seoul 23616000 37.56N 126.99E Korea(South)
Jakarta 28019545 6.18S 106.83E Indonesia
Tokyo 35682460 35.67N 139.77E Japan
Peking 19612368 39.91N 116.39E China
# -*- coding: utf-8 -*-
# Written by Vamei, http://www.cnblogs.com/vamei/
# Feel free to use or modify this script.
from mpl_toolkits.basemap import Basemap
import matplotlib.pyplot as plt
import numpy as np
#============================================
# read data
names = []
pops = []
lats = []
lons = []
countries = []
for line in open("major_city.txt","r"):
info = line.split()
names.append(info[0])
pops.append(float(info[1]))
lat = float(info[2][:-1])
if info[2][-1] == 'S': lat = -lat
lats.append(lat)
lon = float(info[3][:-1])
if info[3][-1] == 'W': lon = -lon + 360.0
lons.append(lon)
country = info[4]
countries.append(country)
#============================================
# set up map projection with
# use low resolution coastlines.
map = Basemap(projection='ortho',lat_0=35,lon_0=120,resolution='l')
# draw coastlines, country boundaries, fill continents.
map.drawcoastlines(linewidth=0.25)
map.drawcountries(linewidth=0.25)
# draw the edge of the map projection region (the projection limb)
map.drawmapboundary(fill_color='#689CD2')
# draw lat/lon grid lines every 30 degrees.
map.drawmeridians(np.arange(0,360,30))
map.drawparallels(np.arange(-90,90,30))
# Fill continent wit a different color
map.fillcontinents(color='#BF9E30',lake_color='#689CD2',zorder=0)
# compute native map projection coordinates of lat/lon grid.
x, y = map(lons, lats)
max_pop = max(pops)
# Plot each city in a loop.
# Set some parameters
size_factor = 80.0
y_offset = 15.0
rotation = 30
for i,j,k,name in zip(x,y,pops,names):
size = size_factor*k/max_pop
cs = map.scatter(i,j,s=size,marker='o',color='#FF5600')
plt.text(i,j+y_offset,name,rotation=rotation,fontsize=10)
plt.title('Major Cities in Asia & Population')
plt.show()
四.总结
简单纪念下,CSDN阅读量即将破千万,全网粉丝近30万。十年啊,近700篇文章,确实可以说一句:这就是我20到30岁的青春,这里既有技术博客,也有娜璋珞一家的故事,我们的爱情史,也见证了一个自幼受贵州大山熏陶的学子慢慢成长,让我认识了许许多多的博友。如图的苏老师,受尽挫折,博士毕业,回到家乡玉林成为了一名大学老师,今天更是自费建成了化学实验室,只想将自己的所学所感传递给他的学生。十年,我在CSDN认识了许多这样的博友、老师和大佬,我们从未谋面,我们天南地北,但相互鼓励,苔花如米小,也学牡丹开。
最后,感谢CSDN,这些年让我骗了很多礼物,更感谢每一位阅读过娜璋故事,每一位给我技术博客点赞的读者。也希望大家记住一个叫Eastmount的分享者,对,不是什么专家,也不是什么大佬,就是一个默默撰写博客的技术分享者,因为爱所以写(今年太忙写得很少很少)。我还将在CSDN写二十年,三十年,一辈子,也将记录我们一家的故事。好想继续抒写我们的故事,但太忙太忙,毕业后再好好写吧。
希望能早日毕业,回到家乡贵州继续当个教书匠,感觉好多要分享的博客,好多要上的课程,好多要开源的代码,好多要学习的知识,期待再次站在讲台前的那一天。继续沉下心去学习,虽菜但勤,感恩遇见,继续加油,晚安娜!
(By:娜璋之家 2022-07-13 夜于地球)
原文始发于微信公众号(娜璋AI安全之家):[Python从零到壹] 十八.可视化分析之Basemap地图包入门详解
- 左青龙
- 微信扫一扫
-
- 右白虎
- 微信扫一扫
-
评论