小能豆

ImportError:无法从“folium.utilities”导入名称“_validate_location”

python

我尝试了这段代码:

from folium.plugins import MarkerCluster

# let's start again with a clean copy of the map of San Francisco
sanfran_map = folium.Map(location = [latitude, longitude], zoom_start = 12)

# instantiate a mark cluster object for the incidents in the dataframe
incidents = plugins.MarkerCluster().add_to(sanfran_map)

# loop through the dataframe and add each data point to the mark cluster
for lat, lng, label, in zip(df_incidents.Y, df_incidents.X, df_incidents.Category):
    folium.Marker(
        location=[lat, lng],
        icon=None,
        popup=label,
    ).add_to(incidents)

# display map
sanfran_map

import此外,我尝试了这样的第一部分from folium import plugins,但它总是抛出错误。我希望你能帮助我


阅读 63

收藏
2023-11-14

共1个答案

小能豆

您似乎正在尝试使用库MarkerCluster中的插件folium,但遇到错误。该错误可能是由于您没有正确导入folium库或plugins模块所致。

以下是如何导入folium和使用该MarkerCluster插件的示例:

import folium
from folium import plugins

# Assume latitude and longitude are defined
latitude = 37.7749
longitude = -122.4194

# Assuming df_incidents is your DataFrame with incident data
# Replace this with your actual DataFrame
df_incidents = ...

# Let's start with a clean copy of the map of San Francisco
sanfran_map = folium.Map(location=[latitude, longitude], zoom_start=12)

# Instantiate a MarkerCluster object for the incidents in the dataframe
incidents = plugins.MarkerCluster().add_to(sanfran_map)

# Loop through the dataframe and add each data point to the MarkerCluster
for lat, lng, label in zip(df_incidents.Y, df_incidents.X, df_incidents.Category):
    folium.Marker(
        location=[lat, lng],
        icon=None,
        popup=label,
    ).add_to(incidents)

# Display the map
sanfran_map

在运行此代码之前,请确保您已经安装了该folium库。如果您尚未安装它,可以通过运行以下命令来安装:

pip install folium

如果您仍然遇到问题,请提供有关您看到的具体错误消息的更多详细信息。

2023-11-14