当我更改投影时,Cartopy 不会绘制海岸线

问题描述 投票:0回答:1

我正在尝试绘制地中海北部的海岸线。

以下脚本使用 PlateCarre 投影,有效:

import matplotlib.pyplot as plt
import cartopy.crs as ccrs
from sys import exit as sys_exit


def main():
    plt.figure(dpi=600)
    ax = plt.axes(projection=ccrs.PlateCarree())

    lon1, lon2, lat1, lat2 = 12., 16., 43.3, 46.
    ax.set_extent([lon1, lon2, lat1, lat2], crs=ccrs.PlateCarree())
    ax.coastlines()

    plt.savefig('test.pdf')
    plt.close()

    return 0


if __name__ == '__main__':
    sys_exit(main())

如果我用 ccrs.Mercator 替换 ccrs.PlateCarree,我会得到一个空图像。 这是带有替换投影的脚本:

import matplotlib.pyplot as plt
import cartopy.crs as ccrs
from sys import exit as sys_exit


def main():
    plt.figure(dpi=600)
    ax = plt.axes(projection=ccrs.Mercator())

    lon1, lon2, lat1, lat2 = 12., 16., 43.3, 46.
    ax.set_extent([lon1, lon2, lat1, lat2], crs=ccrs.Mercator())
    ax.coastlines()

    plt.savefig('test.pdf')
    plt.close()

    return 0


if __name__ == '__main__':
    sys_exit(main())

你能告诉我我做错了什么吗?

python matplotlib cartopy
1个回答
0
投票

墨卡托坐标系不使用度数。如果您仍然想指定以度为单位的范围,可以使用

PlateCarree
crs 来实现。

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

plt.figure()
ax = plt.axes(projection=ccrs.Mercator())

lon1, lon2, lat1, lat2 = 12., 16., 43.3, 46.
ax.set_extent([lon1, lon2, lat1, lat2], crs=ccrs.PlateCarree())
ax.coastlines()

plt.show()

© www.soinside.com 2019 - 2024. All rights reserved.