Cartopy在极性区域的小区域图中失败

2024-04-29 03:41:17 发布

您现在位置:Python中文网/ 问答频道 /正文

我想制作简单的静态地图,用于期刊论文。我在北极工作,我制作了很多地图图像,显示设备布局、船只轨迹以及震源和接收器位置。我不能用卡托皮做这件事。例如,在例如74.5N的中纬度,1度纬度(74到75N)和2.5度经度(-92.5到-90.0)。你不能让海岸线正常运作。这张地图通常是空的,但它应该显示出德文岛的部分海岸线。如果我将绘图设为一个更大的区域(比如30度乘以30度),它可以工作,但您会看到图形窗口中显示的坐标没有正确对齐。X、Y值与图形轴匹配,但括号中的lat、lon值会移动。在最坏的情况下,lat、LON的温度为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)


Tags: theimportas地图pltextentprojlon
1条回答
网友
1楼 · 发布于 2024-04-29 03:41:17

一些明显的错误:

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

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

相关问题 更多 >