【发布时间】: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