python 绘制世界地图,添加海岸线、陆地、投影

简介: tips:最近在学习python的模块:matplotlib为画全球的要素分布做准备在此之前,先学习一下如何绘制世界地图,以及一些投影的使用。

tips:最近在学习python的模块:matplotlib

为画全球的要素分布做准备

在此之前,先学习一下如何绘制世界地图,以及一些投影的使用。


首先引用如下三个库


import cartopy.crs as ccrs
import matplotlib.pyplot as plt
import cartopy.feature as cfeature


其中,Cartopy是一个Python库,用于地理空间数据处理,以便生成地图和其他地理空间数据分析。


matplotlib.pyplot是matplotlib基于状态的接口。 它提供了类似于MATLAB的绘图方式。


pyplot主要用于交互式绘图和程序化绘图生成的简单情况:


import numpy as np
import matplotlib.pyplot as plt
x = np.arange(0, 5, 0.1)
y = np.sin(x)
plt.plot(x, y)


成图如下:


20210422202955745.png


为了使地图更加多样化,可以引入cartopy.feature ,内置了许多有用的功能。这些“默认功能”的分辨率为110m。


cartopy.feature.BORDERS Country boundaries#添加城市边界
cartopy.feature.COASTLINE Coastline, including major islands cartopy.feature.LAKES Natural and artificial lakes
cartopy.feature.LAND Land polygons, including major islands
cartopy.feature.OCEAN Ocean polygons
cartopy.feature.RIVERS Single-line drainages, including lake centerlines
cartopy.feature.STATES (limited to the United States at this scale)


下面开始绘图,在绘图之前,要先准备一个画板,然后设置你需要的投影方式。


import matplotlib.pyplot as plt
import cartopy.crs as ccrs
import cartopy.feature as cfeature
fig=plt.figure(figsize=(20,15))#设置一个画板,将其返还给fig
ax = fig.add_subplot(1, 1, 1, projection=ccrs.PlateCarree())
plt.show()
# 在一个画板中添加子图,并设置投影方式。1,1,1分别代表行、列、在网格中子图位置。并将其返回给ax



202104222040017.png


下面添加海岸线:


import matplotlib.pyplot as plt
import cartopy.crs as ccrs
import cartopy.feature as cfeature
fig=plt.figure(figsize=(20,15))#设置一个画板,将其返还给fig
ax = fig.add_subplot(1, 1, 1, projection=ccrs.PlateCarree())
ax.coastlines()
plt.show()



20210422204242714.png


当当当,这样就有点雏形啦,下面可以再加上一点自己感兴趣的东西,比如给他加上地球背景。加上如下一句代码试试发生了什么?


ax.stock_img()#添加地球背景


20210422204437741.png


嗯~真不错!

还记得开始前介绍的cartopy.feature吗?使图片更加多元就靠他啦,加上你想要的元素,我这里加入了海洋,陆地,湖泊,河流,网格线,让我们看看效果:


ax.add_feature(cfeature.OCEAN)
ax.add_feature(cfeature.LAND, edgecolor='black')
ax.add_feature(cfeature.LAKES, edgecolor='black')#边缘为黑色
ax.add_feature(cfeature.RIVERS)
rivers_110m = cfeature.NaturalEarthFeature('physical', 'rivers_lake_centerlines', '110m')
ax.add_feature(rivers_110m, facecolor='None', edgecolor='b')
ax.gridlines(draw_labels=True, xlocs=[-179, -90, 0, 90,179])#添加网格线,true


20210422204751697.png


乍一看感觉不是很明显,这是因为我们再次之前已经加上地球背景了,我们可以将那句代码去掉再看看效果。


import matplotlib.pyplot as plt
import cartopy.crs as ccrs
import cartopy.feature as cfeature
fig=plt.figure(figsize=(20,15))#设置一个画板,将其返还给fig
ax = fig.add_subplot(1, 1, 1, projection=ccrs.PlateCarree())
ax.coastlines()
ax.add_feature(cfeature.OCEAN)
ax.add_feature(cfeature.LAND, edgecolor='black')
ax.add_feature(cfeature.LAKES, edgecolor='black')#边缘为黑色
ax.add_feature(cfeature.RIVERS)
rivers_110m = cfeature.NaturalEarthFeature('physical', 'rivers_lake_centerlines', '110m')
ax.add_feature(rivers_110m, facecolor='None', edgecolor='b')
ax.gridlines(draw_labels=True, xlocs=[-179, -90, 0, 90,179])#添加网格线,true


20210422204922624.png


这样子就比较明了啦!

分享一些其他的小技巧以及投影:


set_global - zoom the map out as much as possible#尽可能缩小地图
set_extent - zoom the map to the given bounding box#将地图缩放到给定的边界框
gridlines - add a graticule (and optionally labels) to the axes#在轴上添加刻度线(和可选的标签)
coastlines - add Natural Earth coastlines to the axes#给轴上添加自然的地球海岸线
stock_img - add a low-resolution Natural Earth background image to the axes#将低分辨率自然地球背景图像添加到轴
imshow - add an image (numpy array) to the axes#向轴添加图像(numpy数组)
add_geometries - add a collection of geometries (Shapely) to the axes#向轴添加几何形状(形状)集合
projections = [ccrs.PlateCarree(),
               ccrs.Robinson(),
               ccrs.Mercator(),
               ccrs.Orthographic(),
               ccrs.InterruptedGoodeHomolosine()
              ]
              #具体作用可以自己尝试!!!


最后的最后,把绘制部分区域的代码也贴上啦


import matplotlib.pyplot as plt
import cartopy.feature as cfeature
import numpy as np
import cartopy.crs as ccrs
central_lat = 37.5
central_lon = -96
extent = [28, 45, -25, 2]
central_lon = np.mean(extent[:2])
central_lat = np.mean(extent[2:])
fig=plt.figure(figsize=(20, 15))
ax = fig.add_subplot(1, 1, 1, projection=ccrs.EquidistantConic(central_lon, central_lat))
ax.set_extent(extent)
ax.add_feature(cfeature.OCEAN)
ax.add_feature(cfeature.LAND, edgecolor='black')
ax.add_feature(cfeature.LAKES, edgecolor='black')
ax.add_feature(cfeature.RIVERS)
ax.gridlines()


20210422210232576.png


一个努力学习python的海洋小白

水平有限,欢迎指正!!!

欢迎评论、收藏。


相关文章
|
5月前
|
数据可视化 API 算法框架/工具
Python用稀疏、高斯随机投影和主成分分析PCA对MNIST手写数字数据进行降维可视化
Python用稀疏、高斯随机投影和主成分分析PCA对MNIST手写数字数据进行降维可视化
|
编解码 Python
python--海温、OLR数据分布做显著性检验,绘制空间分布并打点
使用python对海洋气象数据做显著性检验,并绘制空间pattern
python--海温、OLR数据分布做显著性检验,绘制空间分布并打点
|
测试技术 Python
Python:使用nltk统计词频并绘制统计图
Python:使用nltk统计词频并绘制统计图
118 0
Python:使用nltk统计词频并绘制统计图
|
Linux 定位技术 Python
python--使用cnmaps绘制省界地图(快速上手,简单有效)
cnmaps是一个致力于让中国地图的获取和使用更丝滑的python扩展包。
python--使用cnmaps绘制省界地图(快速上手,简单有效)
|
存储 数据处理 Python
python--对站点数据做EOF并做插值绘制填色图
最近,师弟在学习使用python复现毕设论文,正好之前没有处理过站点数据,也没咋用过EOF,特此记录下使用python处理站的数据的过程。
python--对站点数据做EOF并做插值绘制填色图
python--循环绘制ERA5风场的空间分布图
使用python封装绘图函数循环绘制ERA5风场资料的空间分布图
python--循环绘制ERA5风场的空间分布图
|
存储 Python
python绘图--由逐日风场数据计算月平均风场数据并绘制二维填色图
python绘图--由逐日风场数据计算月平均风场数据并绘制二维填色图
python绘图--由逐日风场数据计算月平均风场数据并绘制二维填色图
python 循环绘制子图时,设置共享x、y轴
通常在阅读文献时,发现对于一些图片的绘制时,如果存在多个子图,通常为了美观、简洁,只保留最后一列的以及最左一侧的子图的刻度
python 循环绘制子图时,设置共享x、y轴
python绘图——绘制正负区分的柱形图[ax.bar()]
python绘图——绘制正负区分的柱形图[ax.bar()]
python绘图——绘制正负区分的柱形图[ax.bar()]