【问题标题】:Cartopy fails with small regional plots in polar regionsCartopy 在极地地区的小区域地块上失败了
【发布时间】:2020-11-23 20:44:14
【问题描述】:

我想制作用于期刊论文的简单静态地图。我在北极工作,制作了很多地图图像,显示设备布局、船只轨迹以及源和接收器位置。我不能在 CARTOPY 中做到这一点。例如,在 74.5N 的中纬度处,纬度为 1 度(74 到 75N),经度为 2.5 度(-92.5 到 -90.0)。你不能让海岸线正常工作。该地图通常是空白的,但它应该显示了 NU 德文岛海岸线的一部分。如果我将绘图设为更大的区域(例如 30 度乘 30 度),它可以工作,但您会看到图形窗口中显示的坐标没有正确排列。 X、Y 值与图形轴匹配,但括号中的纬度、经度值发生了偏移。在最坏的情况下,lat、lons 出现在 0.00n 度甚至近半个世界之外。

我尝试了多种调用范围的方法。不同的预测。似乎没有任何效果。

#!/usr/bin/env python3
# -*- coding: utf-8 -*-

import cartopy.crs as ccrs
import matplotlib.pyplot as plt
from cartopy.feature import NaturalEarthFeature
from cartopy.mpl.ticker import LongitudeFormatter, LatitudeFormatter
import numpy as np
import matplotlib.ticker as mticker


# the limits of the map
# extent = (-100., -50.0, 60.0, 80.0)  # try this, you'll get a shifted plot on northern Alaska
extent = (-92.5, -90.0, 74.0, 75.0)   # try this, you'll get a blank plot. axes shifted badly.

# set the projection type
c_lon, c_lat = (extent[0] + extent[1])/2., (extent[2] + extent[3])/2.
proj = ccrs.PlateCarree(central_longitude=c_lon)
# proj = ccrs.Mercator(c_lon)    # I've tried these as well
# proj = ccrs.Orthographic(c_lon, c_lat)

gax = plt.axes(projection=proj)
gax.set_extent(extent)
gax.set_ylim((extent[2], extent[3]))
gax.set_xlim((extent[0], extent[1]))

# now add the coastline. This only works for big maps. Small regions fail.
coastline_10m = NaturalEarthFeature(category='physical', name='coastline', \
                    facecolor='none', scale='10m')
gax.add_feature(coastline_10m, edgecolor='gray')

# draw a grid with labelled lat and lon. Suppress ticklabels on the top and right.
gl = gax.gridlines(crs=proj, draw_labels=True) # only works with PlateCarree()
gl.xlabels_top = None
gl.ylabels_right = False

# now we put labels on the X and Y axes. You have to move these around manually.
gax.text(-0.2, 0.55, 'Latitude [Deg]', va='bottom', ha='center',
        rotation='vertical', rotation_mode='anchor',
        transform=gax.transAxes)
gax.text(0.5, -0.12, 'Longitude [Deg]', va='bottom', ha='center',
        rotation='horizontal', rotation_mode='anchor',
        transform=gax.transAxes)

# approximately correct for the aspect ratio
plt.gca().set_aspect(1.0/(np.cos(np.pi*(extent[2] + extent[3])/(2.*180.))))
plt.show()

macOS Catalina、Anaconda python3.8.3、IPython 7.19.0、cartopy 0.17(支持的最高版本。Anaconda 说是 0.18,但它安装的是 0.17)。

【问题讨论】:

    标签: python matplotlib cartopy


    【解决方案1】:

    一些明显的错误:

    1. set_extent() 需要选项crs=ccrs.PlateCarree()
    2. set_xlim()set_ylim 需要数据(地图投影)坐标。

    set_xlim()set_ylim 更改了您在上一行中对set_extent() 所做的操作。它们必须正确使用。大多数情况下,不应使用它们。

    【讨论】:

    • 谢谢。你把我引对了!我注释掉了 ylim 和 xlim 并且它起作用了。尽管经度为 +-1.25 度并以这种方式标记。我以前试过这个,它对我不起作用。我不知道发生了什么变化,除了我删除了 Cartopy 并重新安装了它。也许这解决了一个问题??
    • 我发现如果你在set_extent()中设置crs=proj,它又会失败。然后它会正确标记经度,尽管格式很差,但它无法绘制海岸线。肯定有一些奇怪的事情发生。如果我将 central_longitude 设置为零,我会得到海岸线和正确的经度标签。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-03-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多