python - 使用 cartopy 在背景地图上绘制数组
问题描述
我正在尝试使用 cartopy 在背景地图图块上绘制一个 numpy 数组。包含背景图时,阵列不可见。
我正在使用cimgt
和添加背景地图图块geo_axes.add_image()
。这种方法在用 绘制点之前对我有用plt.scatter()
。我尝试了几种投影(PlateCarree、Mercator 和 EPSG32630)和地图图块(OSM、GoogleTiles)。该数组包含 np.nans 和浮点数。
这是我的代码:
import numpy as np
import matplotlib.pyplot as plt
import cartopy.crs as ccrs
import cartopy.io.img_tiles as cimgt
# array creation
array = np.asarray([[1, np.nan, np.nan], [1, 1, 1], [2, 2, 1]])
x_coords = np.asarray([690000, 691000, 692000])
y_coords = np.asarray([4958000, 4959000, 496000])
# create figure
fig = plt.figure(figsize=(8, 6), dpi=100)
# create geo axes
projection = ccrs.epsg(32630)
geo_axes = plt.subplot(projection=projection)
# add open street map background
# when commenting the two following lines, the data array is plotted correctly
osm_background = cimgt.OSM()
geo_axes.add_image(osm_background, 14)
# plot dataset
plt.imshow(
array,
origin="upper",
extent=(x_coords[0], x_coords[1], y_coords[0], y_coords[1]),
transform=projection,
)
# show plot
plt.show()
我似乎无法找到导致问题的原因。有没有人遇到过这种情况,或者任何人都可以看到我做错了什么?
解决方案
您需要一些技巧来显示所有绘制的特征。这是更新您的相关代码,以及显示(OSM)背景和数组图像的输出图。
# plot dataset
plt.imshow(
array,
origin="upper",
extent=(x_coords[0], x_coords[1], y_coords[0], y_coords[1]),
transform=projection,
alpha=0.25, # allows the background image show-through
zorder=10 # make this layer on top
)
# draw graticule and labels
geo_axes.gridlines(color='lightgrey', linestyle='-', draw_labels=True)
结果:
推荐阅读
- c# - 无法写入自定义事件日志
- java - 即使应用程序未运行,如何每天更新 TextView?
- python - 使用 LidarLite-python 代码进行距离测量
- python - AttributeError:“unicode”对象没有属性“css”
- join - 如何在mysqli中加入一个表,然后在表内加入表?
- node.js - 无法在 nodejs 上启动服务器
- python - 在自定义类型映射中重用 SWIG 映射
- python - 反转数据框中的每 n 行
- python - Django Sqlite3 编程错误
- javascript - 如何在 three.js 中从 BufferGeometry 绘制 2D/3D 网格