gpt4 book ai didi

python - OpenStreetMap 背景上的 Cartopy 热图

转载 作者:行者123 更新时间:2023-11-30 22:14:06 25 4
gpt4 key购买 nike

我使用 cartopy 创建了一个开放街道 map 图:

from __future__ import division
import numpy as np
import matplotlib as mpl
import matplotlib.pyplot as plt
import cartopy.crs as ccrs
import cartopy.io.img_tiles as cimgt

request = cimgt.OSM()
extent = [-89, -88, 41, 42]

ax = plt.axes(projection=request.crs)
ax.set_extent(extent)

ax.add_image(request, 8)
plt.show()

enter image description here

现在我还有一个经度和纬度点的列表。如何在街道 map 上叠加这些经度和纬度点的热图?

我尝试过使用 hist2d,但这不起作用。

lons = (-88 --89)*np.random.random(100)+-89
lats = (41 - 42)*np.random.random(100)+42
ax.hist2d(lons,lats)
plt.show()

但这行不通。

我猜我必须在绘图命令中的某个地方抛出一个转换参数?但我不确定如何去做。

谢谢!

最佳答案

您对坐标转换的需求是正确的。这是带有结果图的工作代码。

import numpy as np 
import matplotlib as mpl
import matplotlib.pyplot as plt
import cartopy.crs as ccrs
import cartopy.io.img_tiles as cimgt

request = cimgt.OSM()
fig, ax = plt.subplots(figsize=(10,16),
subplot_kw=dict(projection=request.crs))
extent = [-89, -88, 41, 42] # (xmin, xmax, ymin, ymax)
ax.set_extent(extent)
ax.add_image(request, 8)

# generate (x, y) centering at (extent[0], extent[2])
x = extent[0] + np.random.randn(1000)
y = extent[2] + np.random.randn(1000)

# do coordinate conversion of (x,y)
xynps = ax.projection.transform_points(ccrs.Geodetic(), x, y)

# make a 2D histogram
h = ax.hist2d(xynps[:,0], xynps[:,1], bins=40, zorder=10, alpha=0.5)
#h: (counts, xedges, yedges, image)

cbar = plt.colorbar(h[3], ax=ax, shrink=0.45, format='%.1f') # h[3]: image

plt.show()

结果图:enter image description here

编辑 1

当使用plt.subplots()创建ax时,它定义了特定的投影。在本例中,投影由关键字 projection=request.crs 定义。要在 ax 上绘制某些内容,您必须使用其坐标系。

坐标转换是通过语句中的函数transform_points()完成的

xynps=ax.projection.transform_points(ccrs.Geodetic(), x, y)

哪里

  • (x, y) 是(经度、纬度)值的列表,
  • ccrs.Geodetic() 表示值(经度、纬度)以度为单位

返回值xynps是 map 坐标数组。它有 2 列 x 和 y,位于当前 ax 使用的适当坐标系中。

关于python - OpenStreetMap 背景上的 Cartopy 热图,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50611018/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com