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

您所在的位置:网站首页 地球平面图怎么画简单 python 绘制世界地图,添加海岸线、陆地、投影

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

2024-07-18 04:19:30| 来源: 网络整理| 查看: 265

python 绘制世界地图,添加海岸线、陆地、投影等 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)

成图如下: 在这里插入图片描述为了使地图更加多样化,可以引入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

在这里插入图片描述 下面添加海岸线:

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()

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

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

在这里插入图片描述嗯~真不错! 还记得开始前介绍的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

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

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

可以这样子就比较明了啦! 分享一些其他的小技巧以及投影:

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()

在这里插入图片描述

一个努力学习python的海洋小白 水平有限,欢迎指正!!! 欢迎评论、收藏。


【本文地址】

公司简介

联系我们

今日新闻


点击排行

实验室常用的仪器、试剂和
说到实验室常用到的东西,主要就分为仪器、试剂和耗
不用再找了,全球10大实验
01、赛默飞世尔科技(热电)Thermo Fisher Scientif
三代水柜的量产巅峰T-72坦
作者:寞寒最近,西边闹腾挺大,本来小寞以为忙完这
通风柜跟实验室通风系统有
说到通风柜跟实验室通风,不少人都纠结二者到底是不
集消毒杀菌、烘干收纳为一
厨房是家里细菌较多的地方,潮湿的环境、没有完全密
实验室设备之全钢实验台如
全钢实验台是实验室家具中较为重要的家具之一,很多

推荐新闻


    图片新闻

    实验室药品柜的特性有哪些
    实验室药品柜是实验室家具的重要组成部分之一,主要
    小学科学实验中有哪些教学
    计算机 计算器 一般 打孔器 打气筒 仪器车 显微镜
    实验室各种仪器原理动图讲
    1.紫外分光光谱UV分析原理:吸收紫外光能量,引起分
    高中化学常见仪器及实验装
    1、可加热仪器:2、计量仪器:(1)仪器A的名称:量
    微生物操作主要设备和器具
    今天盘点一下微生物操作主要设备和器具,别嫌我啰嗦
    浅谈通风柜使用基本常识
     众所周知,通风柜功能中最主要的就是排气功能。在

    专题文章

      CopyRight 2018-2019 实验室设备网 版权所有 win10的实时保护怎么永久关闭