Cartopy 失败,极地地区的小区域地块
Cartopy fails with small regional plots in polar regions
我想制作简单的静态地图用于期刊论文。我在北极工作,制作了很多地图图像,显示设备布局、船只轨迹以及源和接收器位置。我不能在 CARTOPY 中做到这一点。例如,1 度纬度(74 到 75N)和 2.5 度经度(-92.5 到 -90.0)在中纬度 74.5N。你无法让海岸线正常运转。地图通常是空的,但它应该显示新州德文岛的部分海岸线。如果我将绘图设为更大的区域(大约 30 度 x 30 度),它会起作用,但您会看到图表 window 中显示的坐标没有正确对齐。 X、Y 值与图形轴匹配,但括号中的纬度、经度值发生了偏移。在最坏的情况下,纬度、经度显示为 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)。
一些明显的错误:
set_extent()
需要选项 crs=ccrs.PlateCarree()
set_xlim()
和set_ylim
需要数据(地图投影)坐标。
以及set_xlim()
和set_ylim
改变了你在上一行中用set_extent()
所做的事情。必须正确使用它们。你的大部分情况,他们不应该被使用。
我想制作简单的静态地图用于期刊论文。我在北极工作,制作了很多地图图像,显示设备布局、船只轨迹以及源和接收器位置。我不能在 CARTOPY 中做到这一点。例如,1 度纬度(74 到 75N)和 2.5 度经度(-92.5 到 -90.0)在中纬度 74.5N。你无法让海岸线正常运转。地图通常是空的,但它应该显示新州德文岛的部分海岸线。如果我将绘图设为更大的区域(大约 30 度 x 30 度),它会起作用,但您会看到图表 window 中显示的坐标没有正确对齐。 X、Y 值与图形轴匹配,但括号中的纬度、经度值发生了偏移。在最坏的情况下,纬度、经度显示为 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)。
一些明显的错误:
set_extent()
需要选项crs=ccrs.PlateCarree()
set_xlim()
和set_ylim
需要数据(地图投影)坐标。
以及set_xlim()
和set_ylim
改变了你在上一行中用set_extent()
所做的事情。必须正确使用它们。你的大部分情况,他们不应该被使用。