ITPub博客

首页 > 应用开发 > Python > python中Cartopy绘图使用技巧

python中Cartopy绘图使用技巧

原创 Python 作者:mingtian66 时间:2024-09-02 16:10:23 0 删除 编辑

Cartopy 是一个用于地理空间数据可视化的 Python 库,特别适合与 Matplotlib 结合使用。它支持绘制地图、添加地理信息、绘制图形等功能。下面是一些 Cartopy 绘图的技巧和最佳实践,帮助你更高效地使用这个库。

1. 基础地图绘制


首先,确保你已经安装了 Cartopy 和 Matplotlib,可以使用以下命令进行安装:


bash


pip install cartopy matplotlib


基本示例:


python


import matplotlib.pyplot as plt

import cartopy.crs as ccrs


# 创建绘图和地图轴

fig, ax = plt.subplots(subplot_kw={'projection': ccrs.PlateCarree()})


# 绘制地球的轮廓

ax.coastlines()


# 设置标题

ax.set_title('Basic Map')


plt.show()


2. 添加海岸线和国界


python


import matplotlib.pyplot as plt

import cartopy.crs as ccrs


fig, ax = plt.subplots(subplot_kw={'projection': ccrs.PlateCarree()})


# 绘制海岸线

ax.coastlines()


# 绘制国界

ax.add_feature(cartopy.feature.BORDERS)


# 绘制海洋、陆地和湖泊

ax.add_feature(cartopy.feature.LAND, edgecolor='black')

ax.add_feature(cartopy.feature.OCEAN)

ax.add_feature(cartopy.feature.LAKES, edgecolor='black')


plt.show()


3. 绘制数据


示例:绘制点数据:


python


import matplotlib.pyplot as plt

import cartopy.crs as ccrs


fig, ax = plt.subplots(subplot_kw={'projection': ccrs.PlateCarree()})


# 绘制海岸线

ax.coastlines()


# 数据点

lons = [10, 20, 30]

lats = [50, 60, 70]

ax.scatter(lons, lats, color='red', s=100, transform=ccrs.PlateCarree())


plt.show()


示例:绘制等值线:


python


import numpy as np

import matplotlib.pyplot as plt

import cartopy.crs as ccrs

import cartopy.feature as cfeature


# 创建数据

lon = np.linspace(-180, 180, 360)

lat = np.linspace(-90, 90, 180)

lon, lat = np.meshgrid(lon, lat)

z = np.sin(np.radians(lat)) ** 2


fig, ax = plt.subplots(subplot_kw={'projection': ccrs.PlateCarree()})

ax.coastlines()

ax.add_feature(cfeature.BORDERS)

contour = ax.contourf(lon, lat, z, transform=ccrs.PlateCarree(), cmap='coolwarm')

plt.colorbar(contour, ax=ax, orientation='vertical')

ax.set_title('Contour Plot')


plt.show()


4. 设置地图投影


Cartopy 支持多种地图投影,你可以通过设置 projection 参数来选择不同的投影方式:


python


import matplotlib.pyplot as plt

import cartopy.crs as ccrs


# 使用 Mercator 投影

fig, ax = plt.subplots(subplot_kw={'projection': ccrs.Mercator()})

ax.coastlines()

ax.set_title('Mercator Projection')


plt.show()


5. 绘制标记和标签


绘制标记:


python


import matplotlib.pyplot as plt

import cartopy.crs as ccrs


fig, ax = plt.subplots(subplot_kw={'projection': ccrs.PlateCarree()})

ax.coastlines()


# 绘制标记

lons = [-75, -80, -85]

lats = [35, 40, 45]

ax.plot(lons, lats, 'o', color='blue', transform=ccrs.PlateCarree())


plt.show()


添加标签:


python


import matplotlib.pyplot as plt

import cartopy.crs as ccrs


fig, ax = plt.subplots(subplot_kw={'projection': ccrs.PlateCarree()})

ax.coastlines()


# 添加标签

lons = [-75, -80, -85]

lats = [35, 40, 45]

for lon, lat in zip(lons, lats):

    ax.text(lon, lat, f'({lon}, {lat})', transform=ccrs.PlateCarree())


plt.show()


6. 处理数据的投影


如果你的数据使用的是不同的投影,你可以使用 cartopy 的 transform 功能将数据转换到地图投影:


python


import matplotlib.pyplot as plt

import cartopy.crs as ccrs

import numpy as np


# 数据

lon = np.linspace(-180, 180, 360)

lat = np.linspace(-90, 90, 180)

lon, lat = np.meshgrid(lon, lat)

data = np.sin(np.radians(lat)) ** 2


# 创建投影

fig, ax = plt.subplots(subplot_kw={'projection': ccrs.AzimuthalEqualArea()})

ax.coastlines()


# 转换数据投影

contour = ax.contourf(lon, lat, data, transform=ccrs.PlateCarree(), cmap='viridis')

plt.colorbar(contour, ax=ax, orientation='vertical')


plt.show()


7. 绘制自定义地理数据


示例:绘制 shapefiles:


python


import matplotlib.pyplot as plt

import cartopy.crs as ccrs

import cartopy.feature as cfeature


fig, ax = plt.subplots(subplot_kw={'projection': ccrs.PlateCarree()})

ax.coastlines()


# 添加 shapefile 数据

shapefile_path = 'path/to/shapefile.shp'

ax.add_geometries(cfeature.ShapelyFeature(shapefile_path, ccrs.PlateCarree()), facecolor='none', edgecolor='black')


plt.show()


8. 处理大规模数据


对于大规模数据,Cartopy 提供了 Datastore 和 Rasterio 等库来高效处理数据,特别是当数据量很大时。

来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/70040412/viewspace-3026602/ ,如需转载,请注明出处,否则将追究法律责任。

请登录后发表评论 登录
全部评论

注册时间:2024-08-07

  • 博文量
    589
  • 访问量
    602807