gpt4 book ai didi

python - 如何使用 .explore() 显示 geopandas 交互式 map

转载 作者:行者123 更新时间:2023-12-02 18:22:10 24 4
gpt4 key购买 nike

我制作了一个 geopandas 数据框,我想使用 geopandas_dataframe.explore() 创建一个交互式 map 。这是我的代码。首先,我创建了 geopandas 数据框,我检查了数据类型,然后尝试使用 gdf.explore() 映射数据框。不幸的是,我的代码没有错误地完成并且没有显示 map 。

代码:

geometry = [Point(xy) for xy in zip(df[1], df[0])]
gdf = geopandas.GeoDataFrame(df, geometry=geometry)
print(gdf.head())
print(gdf.dtypes)
gdf.explore()

输出:

           0         1                  geometry
0 51.858306 5.778404 POINT (5.77840 51.85831)
1 51.858322 5.778410 POINT (5.77841 51.85832)
2 51.858338 5.778416 POINT (5.77842 51.85834)
3 51.858354 5.778422 POINT (5.77842 51.85835)
4 51.858370 5.778429 POINT (5.77843 51.85837)
0 float64
1 float64
geometry geometry
dtype: object

Process finished with exit code 0

为什么我没有 map ?我已经尝试过 gdf.show() 但那不存在。我需要做什么才能显示 geopandas map ?

最佳答案

您使用的是什么 IDE?在 Jupyter Notebook 中,您的代码(稍作修改)适用于我。但是,当我在 PyCharm 中运行它时,我得到“进程已完成,退出代码为 0”,但没有任何情节。

import geopandas as gpd
import pandas as pd
from shapely.geometry import Point

data_dict = {'x': {0: -110.1, 1: -110.2, 2: -110.3, 3: -110.4, 4: -110.5},
'y': {0: 40.1, 1: 40.2, 2: 40.3, 3: 40.4, 4: 40.5}}

df = pd.DataFrame(data_dict)

geometry = [Point(xy) for xy in zip(df['x'], df['y'])]
gdf = gpd.GeoDataFrame(df, geometry=geometry, crs=4326)
print(gdf.head())
print(gdf.dtypes)
gdf.explore()

enter image description here

编辑:看起来您可以将 folium 图保存为 html。这对 PyCharm 有用。

m = gdf.explore()

outfp = r"<your dir path>\base_map.html"

m.save(outfp)

关于python - 如何使用 .explore() 显示 geopandas 交互式 map ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/70759514/

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