gpt4 book ai didi

python - 网格数据值的插值 - python

转载 作者:行者123 更新时间:2023-12-02 08:43:17 24 4
gpt4 key购买 nike

问题:有没有办法从geodataframe中重新采样网格值以绘制更平滑的 map ?

详细信息:我正在使用名为 gdf 的 24x24 网格。网格的每个单元格都有一个和一个id作为属性:

#gdf.head()

id values geometry
0 1 52.390119 POLYGON ((653179.710 6859158.392, 653179.710 6...
1 2 52.390119 POLYGON ((653179.710 6858908.392, 653179.710 6...
2 3 52.390119 POLYGON ((653179.710 6858658.392, 653179.710 6...
3 4 49.592331 POLYGON ((653179.710 6858408.392, 653429.710 6...
4 5 52.390119 POLYGON ((653429.710 6858408.392, 653179.710 6...

这是我绘制 map 时得到的 map 类型:

Initial gridded data

正如您所看到的,图中一个单元格到另一个单元格的值发生了非常剧烈的变化,我想将其平滑化。

有没有办法将每个单元格分成 2 或 3 个子单元格(水平和垂直)以获得更高分辨率的网格,然后插入值以获得平滑的渐变而不是这样? 知道我正在尝试将数据保留为 geodataframe,因为稍后我需要将它们转换为 shapefile

<小时/>

我找到了一种方法,可以通过 plt.imshow() 来完成此操作,因为有一个 interpolation 选项;这将给我我想要的东西,但这只提供图像作为输出,我不能直接用它修改 gdf :

grid = np.array(file.data).reshape(-1, 24)[::-1]

fig, axs = plt.subplots(nrows=1, ncols=2, figsize=(20, 20), subplot_kw={'xticks': [], 'yticks': []})

for ax, interp_method in zip(axs.flat, methods):
ax.imshow(grid, interpolation='lanczos', cmap='RdYlGn_r')

plt.tight_layout()
plt.show()

最佳答案

为了补充我的评论,另一种方法是将网格视为图像并使用 PIL 库:

import numpy as np
from PIL import Image

image = PIL.Image.from_array(grid)
w, h = image.size
ratio = 4
image = image.resize((w*ratio, h*ratio), Image.BILINEAR)
image.show()
grid = np.array(image)

您可以使用different interpolation方法也是如此。要将数据恢复到 pandas 数据框中:

# flatten your grid and get your values back into a column
pd.DataFrame(grid.flatten(), columns=['values'])

# add an id column that starts a 1
df['id'] = df.index + 1

关于python - 网格数据值的插值 - python,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59990795/

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