使用Matplotlib & Cartopy绘制我国台风路径图 |
您所在的位置:网站首页 › 我国台风路径图片 › 使用Matplotlib & Cartopy绘制我国台风路径图 |
夏天一到,沿海地区经常会遭到台风的袭扰,可谓苦不堪言。 之前在公众号做过一个关于我国1945~2015年历史台风统计的可视化展示,发现很多有趣的数据,比如说台风登陆最多的城市是湛江。 image.png 大家可以去翻看历史文章,附有完整代码和数据,有兴趣做些可视化探索。 大数据告诉你,台风最喜欢在我国哪个省市登陆 这次的文章不研究台风数据,而是尝试用Python来绘制台风路径。 主要第三方库用到的主要工具包有pandas、numpy、matplotlib、cartopy、shapely,前三个库大家可能都熟悉,下面介绍下后两个库的使用场景。 ❝cartopy:基于matplotlib的python地理数据处理和可视化库,本文会用来展示地图 shapely: 是一个对几何对象进行操作和分析的Python库,本文用来处理点线数据 ❞ cartopy文档:https://scitools.org.uk/cartopy/docs/latest/ shapely文档:https://shapely.readthedocs.io/en/stable/台风路径数据本文用到的数据是我国2017年所有台风路径,包含了时间、经纬度、强度等关键信息。 由于数据来源网络,没法追溯真实性,仅供练习。 原始数据比较乱,我重新处理了方便使用: 可以看到共有7个字段: ❝台风编号:我国热带气旋编号 日期:具体时间 强度:0~9 纬度:单位0.1度 经度:单位0.1度 中心气压:hPa 中心最大风速:m/s ❞ 绘制地图台风路径需要在地图上展示,那么如何获取地图呢? 方式有很多种,既可以用离线的GeoJson数据,也可以用JPG图片,或者第三方库提供的地图。 我这里用的是cartopy内置的地图数据,可以很方便的修改配置属性。 首先导入本次会用到的所有库: # cartopy:用来获取地图 import cartopy.crs as ccrs import cartopy.feature as cfeature # matplotlib:用来绘制图表 import matplotlib.pyplot as plt # shapely:用来处理点线数据 import shapely.geometry as sgeom import warnings import re import numpy as np import pandas as pd warnings.filterwarnings('ignore') plt.rcParams['font.sans-serif'] = [u'SimHei'] plt.rcParams['axes.unicode_minus'] = False获取我国沿海区域地图: # 通过cartopy获取底图 fig = plt.figure(figsize=(10,10)) ax = fig.add_subplot(1, 1, 1, projection=ccrs.PlateCarree()) # 用经纬度对地图区域进行截取,这里只展示我国沿海区域 ax.set_extent([85,170,-20,60], crs=ccrs.PlateCarree()) # 设置名称 ax.set_title('2017年台风路径图',fontsize=16) # 设置地图属性,比如加载河流、海洋 ax.add_feature(cfeature.LAND) ax.add_feature(cfeature.OCEAN) ax.add_feature(cfeature.COASTLINE) ax.add_feature(cfeature.RIVERS) ax.add_feature(cfeature.COASTLINE) ax.add_feature(cfeature.LAKES, alpha=0.5) # 展示地图 plt.show() 处理数据并可视化先加载数据: typhoonData = pd.read_csv('typhoonData.csv')这个数据集包含了30个台风路径,所以后面要分别进行可视化。 再对数据进行处理,依次提取单个台风路径及其经纬度。 # 先对台风编号进行循环,提取单个台风数据 for typhoonNumber in typhoonData['台风编号'].unique(): typhoon = typhoonData[typhoonData['台风编号']==typhoonNumber] # 再对单个台风数据进行处理,提取经纬度 for typhoonPoint in np.arange(len(typhoon)-1): lat_1 = typhoon.iloc[typhoonPoint,3]/10 lon_1 = typhoon.iloc[typhoonPoint,4]/10 lat_2 = typhoon.iloc[typhoonPoint+1,3]/10 lon_2 = typhoon.iloc[typhoonPoint+1,4]/10 point_1 = lon_1,lat_1 point_2 = lon_2,lat_2 # 最后可视化 ax.add_geometries([sgeom.LineString([point_1,point_2])],crs=ccrs.PlateCarree(),edgecolor='red') # 展示图像 plt.show()能看到所有台风路径都被描绘出来了。 但这里没有区别显示台风强度,一般是在.add_geometries()方法中添加参数调整。 有两种方式: 用颜色区别:不同颜色代表不同强度,参数-edgecolor用线条粗细区别:越粗则强度越高,参数-linewidth颜色区分# 按强度区分颜色 def get_color(level): if level in (0,1): color='#ff00ff' elif level in (2,3): color='#ff00cc' elif level in (4,5): color='#ff0066' elif level in (6,7): color='#ff0033' elif level in (8,9): color='#ccff00' return color # 先对台风编号进行循环,提取单个台风数据 for typhoonNumber in typhoonData['台风编号'].unique(): typhoon = typhoonData[typhoonData['台风编号']==typhoonNumber] # 再对单个台风数据进行处理,提取经纬度 for typhoonPoint in np.arange(len(typhoon)-1): lat_1 = typhoon.iloc[typhoonPoint,3]/10 lon_1 = typhoon.iloc[typhoonPoint,4]/10 lat_2 = typhoon.iloc[typhoonPoint+1,3]/10 lon_2 = typhoon.iloc[typhoonPoint+1,4]/10 point_1 = lon_1,lat_1 point_2 = lon_2,lat_2 # 最后可视化,添加颜色参数 ax.add_geometries([sgeom.LineString([point_1,point_2])],crs=ccrs.PlateCarree(),edgecolor=get_color(typhoon.iloc[typhoonPoint,2])) # 展示图像 plt.show() 线条粗细区分# 先对台风编号进行循环,提取单个台风数据 for typhoonNumber in typhoonData['台风编号'].unique(): typhoon = typhoonData[typhoonData['台风编号']==typhoonNumber] # 再对单个台风数据进行处理,提取经纬度 for typhoonPoint in np.arange(len(typhoon)-1): lat_1 = typhoon.iloc[typhoonPoint,3]/10 lon_1 = typhoon.iloc[typhoonPoint,4]/10 lat_2 = typhoon.iloc[typhoonPoint+1,3]/10 lon_2 = typhoon.iloc[typhoonPoint+1,4]/10 point_1 = lon_1,lat_1 point_2 = lon_2,lat_2 # 最后可视化 ax.add_geometries([sgeom.LineString([point_1,point_2])],crs=ccrs.PlateCarree(),linewidth = typhoon.iloc[typhoonPoint,2],edgecolor='red') # 展示图像 plt.show() 最后上文用比较简单的方式绘制了台风路径图,大家可以尝试换个三维地图,或者用动态显示台风走势... 玩法挺多的,赶紧尝试尝试吧。 |
今日新闻 |
推荐新闻 |
CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3 |