簡體   English   中英

如何顯示 geopandas 交互式 map with.explore()

[英]How to show geopandas interactive map with .explore()

我制作了一個 geopandas dataframe,我想使用geopandas_dataframe.explore()創建一個交互式 map。 這是我的代碼。 首先,我創建 geopandas dataframe,檢查數據類型並嘗試使用 gdf.explore 對 dataframe 進行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()

output:

           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()但那不存在。 我需要做什么才能顯示地理熊貓 map?

您使用的是什么 IDE? 在 Jupyter Notebook 中,您的代碼(稍作修改)對我有用。 但是,當我在 PyCharm 中運行它時,我得到“進程以退出代碼 0 完成”,沒有 plot。

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

data_dict = {'x': {0: 51.858306, 1: 51.858322, 2: 51.858338, 3: 51.858354, 4: 51.85837},
 'y': {0: 5.778404, 1: 5.77841, 2: 5.778416, 3: 5.778422, 4: 5.778429}}

df = pd.DataFrame(data_dict)

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

在此處輸入圖像描述

編輯:看起來您可以將您的葉圖保存到 html。 這從 PyCharm 對我有用。

m = gdf.explore()

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

m.save(outfp)
  • explore()有一個微妙的要求。 GeoDataFrame中的列名需要是字符串。 在您的情況下,它們編號為零和一。
  • 重命名這些列: gdf = gdf.rename(columns={c:str(c) for c in gdf.columns})
import geopandas as gpd
from shapely.geometry import Point
import pandas as pd

# simulate dataframe in question, generates a warning, ignore it
world = gpd.read_file(gpd.datasets.get_path("naturalearth_lowres"))
df = world["geometry"].centroid.apply(lambda g: pd.Series(g.coords[0][::-1]))

geometry = [Point(xy) for xy in zip(df[1], df[0])]
gdf = gpd.GeoDataFrame(data=df, geometry=geometry, crs="epsg:4326")
# explore does not like column names that are not strings...
gdf = gdf.rename(columns={c:str(c) for c in gdf.columns})
# print(gdf.head())
# print(gdf.dtypes)
gdf.explore()

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM