用cartopy画兰博托投影
时间: 2023-11-22 12:55:10 浏览: 282
Cartopy 是一个 Python 库,提供了地图制图工具和地理空间数据处理功能。其中包括许多投影方式,兰博托投影(Lambert Conformal Projection)是其中之一。下面是一个简单的示例,展示如何使用 Cartopy 在兰博托投影下绘制地图。
首先,需要安装 Cartopy 库。可以使用 pip 命令进行安装,命令如下:
```
pip install cartopy
```
接下来,导入必要的库和模块:
```python
import matplotlib.pyplot as plt
import cartopy.crs as ccrs
import cartopy.feature as cfeature
```
然后,创建一个兰博托投影:
```python
proj = ccrs.LambertConformal(central_longitude=120, central_latitude=35)
```
这里的 `central_longitude` 和 `central_latitude` 分别指定了中心经度和中心纬度,可以根据需要进行修改。
接着,创建一个地图对象,并设置投影方式为兰博托投影:
```python
fig = plt.figure(figsize=(8, 8))
ax = fig.add_subplot(1, 1, 1, projection=proj)
```
然后,添加一些地图特征,例如陆地、海洋和国界等:
```python
ax.add_feature(cfeature.LAND)
ax.add_feature(cfeature.OCEAN)
ax.add_feature(cfeature.COASTLINE)
ax.add_feature(cfeature.BORDERS)
```
最后,可以绘制一些数据,例如气象数据、地形数据等。下面是一个简单的示例,绘制了一个简单的圆形:
```python
import numpy as np
theta = np.linspace(0, 2*np.pi, 100)
radius = 30
x = radius * np.cos(theta) + 120
y = radius * np.sin(theta) + 35
ax.plot(x, y, transform=ccrs.PlateCarree(), color='red')
```
这里的 `transform` 参数指定了数据的坐标系,这里使用的是等经纬度投影。
完整的代码如下所示:
```python
import matplotlib.pyplot as plt
import cartopy.crs as ccrs
import cartopy.feature as cfeature
import numpy as np
proj = ccrs.LambertConformal(central_longitude=120, central_latitude=35)
fig = plt.figure(figsize=(8, 8))
ax = fig.add_subplot(1, 1, 1, projection=proj)
ax.add_feature(cfeature.LAND)
ax.add_feature(cfeature.OCEAN)
ax.add_feature(cfeature.COASTLINE)
ax.add_feature(cfeature.BORDERS)
theta = np.linspace(0, 2*np.pi, 100)
radius = 30
x = radius * np.cos(theta) + 120
y = radius * np.sin(theta) + 35
ax.plot(x, y, transform=ccrs.PlateCarree(), color='red')
plt.show()
```
运行代码后,可以得到一个带有圆形的兰博托投影地图。可以根据需要修改代码,添加更多的地图特征和数据。
阅读全文
相关推荐













