Basemap-绘制地图背景(整理自官网)

本文介绍了使用Matplotlib Basemap工具包进行地理绘图的方法,包括海岸线、国家边界、河流等元素的绘制技巧,并提供了如何设置陆地与海洋颜色的示例代码。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

整理自https://matplotlib.org/basemap/users/geography.html

可能用到的方法:

  • drawcoastlines(): draw coastlines.

  • fillcontinents(): 为大陆内部上色。不幸的是,fillmethods并不总是正确的。Matplotlib总是试图填充多边形的内部。在某些情况下,海岸线多边形的内部可能是模糊的,外部可能被填充而不是内部。在这些情况下,推荐的解决方法是使用drawlsmask()方法,使用为陆地和水域指定的不同颜色覆盖图像(见下文)。

  • drawcountries(): draw country boundaries.

  • drawstates(): 划定北美各州界限

  • drawrivers(): draw rivers.

  • drawlsmask(): 绘制高分辨率陆地-海洋掩模作为图像,指定陆地和海洋的颜色。陆海掩模来自GSHHS的海岸线数据,有几种海岸线选项和像素大小可供选择。

  • drawlsmask(land_color=‘0.8’, ocean_color=‘w’, lsmask=None, lsmask_lons=None, lsmask_lats=None, lakes=True, resolution=‘l’, grid=5, **kwargs)

  • bluemarble(): draw a NASA Blue Marble image as a map background.

  • shadedrelief(): draw a shaded relief image as a map background.

  • etopo(): draw an etopo relief image as map background.

  • warpimage(): 使用一个任意的图像作为地图背景。图像必须是全球的,从国际日期变更线向东,南极向北,以经纬线坐标覆盖整个世界。

  • warpimage(image=‘bluemarble’, scale=None, **kwargs)

官网的一个例子:

from mpl_toolkits.basemap import Basemap
import matplotlib.pyplot as plt
# setup Lambert Conformal basemap.
# set resolution=None to skip processing of boundary datasets.
m = Basemap(width=12000000,height=9000000,projection='lcc',
            resolution=None,lat_1=45.,lat_2=55,lat_0=50,lon_0=-107.)
# draw a land-sea mask for a map background.
# lakes=True means plot inland lakes with ocean color.
m.drawlsmask(land_color='coral',ocean_color='aqua',lakes=True)
plt.show()

在这里插入图片描述

import contextily as ctx # 导入contextily库用于添加背景地图 import matplotlib.pyplot as plt # 导入matplotlib.pyplot库用于绘图 # 定义经纬度范围 min_lon, max_lon = 121.4800, 121.5050 # 经度范围 min_lat, max_lat = 31.2300, 31.2450 # 纬度范围 # 创建一个几何框表示指定的区域 geometry = box(min_lon, min_lat, max_lon, max_lat) # 使用box函数创建几何框 gdf = gpd.GeoDataFrame({'geometry': [geometry]}, crs="EPSG:4326") # 创建GeoDataFrame并设置坐标参考系统为WGS 84 # 将坐标系转换为Web Mercator (EPSG:3857)以便与Contextily兼容 gdf_webmercator = gdf.to_crs(epsg=3857) # 转换坐标系为Web Mercator # 设置图形大小 fig, ax = plt.subplots(figsize=(10, 10)) # 创建一个10x10英寸的图形和轴对象 # 绘制几何框 gdf_webmercator.plot(ax=ax, facecolor='none', edgecolor='blue') # 在轴上绘制几何框,无填充颜色,蓝色边框 # 添加背景地图,并设置超时时间和重试次数 try: ctx.add_basemap(ax, source=ctx.providers.OpenStreetMap.Mapnik, zoom=16) # 添加OpenStreetMap背景地图,最大重试次数为3次 except Exception as e: print(f"Failed to add basemap: {e}") # 捕获异常并打印错误信息 # 打印当前轴的限制以检查是否正确 print("Current axis limits:", ax.get_xlim(), ax.get_ylim()) # 打印当前轴的限制 # 移除轴刻度 ax.set_axis_off() # 关闭轴的刻度显示 # 保存图像 plt.savefig("osm_map_visualization.png", bbox_inches='tight', pad_inches=0) # 保存图像到文件,去除周围空白 # 显示图像(可选) plt.show() # 显示图像 图像输出空白,报如下"G:\BaiduSyncdisk\01-Thesis P5-ACO-2\Codes_3D\UAM\envs\Demo_Osm\python.exe" "G:\BaiduSyncdisk\01-Thesis P5-ACO-2\Codes_3D\UAM\CallOsm\VisualizeOsm.py" Failed to add basemap: HTTPSConnectionPool(host='tile.openstreetmap.org', port=443): Max retries exceeded with url: /16/54882/26774.png (Caused by ProxyError('Unable to connect to proxy', SSLError(SSLZeroReturnError(6, 'TLS/SSL connection has been closed (EOF) (_ssl.c:1149)')))) Current axis limits: (13522952.592203382, 13526013.878200196) (3662557.5374864344, 3664705.745485841) Process finished with exit code 0
最新发布
04-02
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值