matplotlib - 如何在特殊投影中的 cartopy 底图上绘制 png 图像?
问题描述
我正在尝试打开一个 png-Image 以将此图像绘制在 cartopy 的底图上。我已经按照以下说明操作: https ://scitools.org.uk/cartopy/docs/v0.15/examples/geostationary.html
import matplotlib.pyplot as plt
import cartopy.crs as ccrs
import cartopy
from PIL import Image
def create_map(image):
res = '10m'
proj = ccrs.NorthPolarStereo(central_longitude=10.0)
img = plt.imread(image)
img_extent = (2.0715, 15.72, 46.9526, 54.5877)
ax = plt.axes(projection = proj)
ax.set_extent = ([3.0889, 17.1128, 46.1827, 55.5482])
land_10m = cfeature.NaturalEarthFeature('physical', 'land', res,
edgecolor = 'face',
facecolor=cfeature.COLORS['land'],
zorder=0)
state_provinces_10m = cfeature.NaturalEarthFeature(category = 'cultural',
name = 'admin_1_states_provinces_lines',
scale = res,
facecolor = none)
ax.add_feature(state_provinces_10m, edgecolor='gray')
ax.add_feature(land_10m)
ax.add_feature(cartopy.feature.BORDERS.with_scale(res), linestyle='-', linewith=1)
ax.add_feature(cartopy.feature.COASTLINE.with_scale(res), linestyle='-')
plt.imshow(img, origin='upper', extent=img_extent, transform = proj)
plt.show()
create_map('image.png')
我的结果是定义范围的底图,但没有我的图像。我做错了什么?
问候
解决方案
您的 transform 参数imshow
几乎可以肯定是不正确的。北极立体投影中的影像范围(2.0715, 15.72, 46.9526, 54.5877)
是靠近北极的一个非常小的区域,不在您想要的地图范围内。从上下文来看,范围似乎是在地理坐标中指定的,在这种情况下,应该transform=ccrs.PlateCarree()
在您的imshow
调用中使用解决方案。
一般来说,我建议始终明确说明您的坐标系是什么,所以我建议
def create_map(image):
res = '10m'
proj = ccrs.NorthPolarStereo(central_longitude=10.0)
img = plt.imread(image)
img_extent = (2.0715, 15.72, 46.9526, 54.5877)
ax = plt.axes(projection = proj)
# EXPLICIT CRS HERE:
ax.set_extent([3.0889, 17.1128, 46.1827, 55.5482], crs=ccrs.PlateCarree())
land_10m = cfeature.NaturalEarthFeature('physical', 'land', res,
edgecolor = 'face',
facecolor=cfeature.COLORS['land'],
zorder=0)
state_provinces_10m = cfeature.NaturalEarthFeature(category = 'cultural',
name = 'admin_1_states_provinces_lines',
scale = res,
facecolor = none)
ax.add_feature(state_provinces_10m, edgecolor='gray')
ax.add_feature(land_10m)
ax.add_feature(cartopy.feature.BORDERS.with_scale(res), linestyle='-', linewith=1)
ax.add_feature(cartopy.feature.COASTLINE.with_scale(res), linestyle='-')
# USE CORRECT CRS HERE
plt.imshow(img, origin='upper', extent=img_extent, transform=ccrs.PlateCarree())
plt.show()
本文档提供有关 Cartopy 中的转换/投影的指南: https ://scitools.org.uk/cartopy/docs/latest/tutorials/understanding_transform.html
推荐阅读
- domain-driven-design - 与患者、牙医、治疗和病史的实体和聚合根源混淆
- flutter - #Flutter Dart 编译器意外退出
- c# - 比较 Voctor.y
- nginx - Janus 的 Nginx 配置
- azure - How to find the superuser for a container in adls2
- c - 有没有办法根据用户输入创建循环?
- java - 如何在 FROM 子句中多次执行数据库函数
- java - java.lang.module.FindException:找不到模块 java.xml.bind
- c++ - 如何循环我的程序以不断询问字母,直到用户输入 Q 结束程序
- javascript - 是否有替代方法可以指定网站中使用的所有资产(如图像等)的确切位置?