Folium 热图未出现
Folium heat map not appearing
我已经尝试将我的代码放入 Jupyter notebook 和 Google Colab 中,但是一旦我解决了代码中的所有错误,我不仅在输出中看到空白屏幕。当我 运行 在将热图数据集放在上面之前仅包含地图的代码时,它可以工作。我使用 https://alysivji.github.io/getting-started-with-folium.html 来构建我的代码。
这是数据集的 link:
https://data.cityofnewyork.us/Public-Safety/NYPD-Arrest-Data-Year-to-Date-/uip8-fykc
我的完整代码如下。
here is what it looks like right now
import pandas as pd
pd.plotting.register_matplotlib_converters()
import matplotlib.pyplot as plt
%matplotlib inline
import seaborn as sns
import numpy as np
import folium
from folium import features
from folium import plugins
from folium.plugins import HeatMap
from folium.plugins import MarkerCluster
print("Setup Complete")
crime_filepath = "arrestdata.csv"
crime_data = pd.read_csv (crime_filepath, index_col = "ARREST_DATE")
crime_data.head()
#this is the section that doesn't work
m = folium.Map([40.7128, -74.0060], zoom_start=10.3)
m #this and the line above properly creats a blank map
for index, row in crime_data.iterrows():
folium.CircleMarker([row['Latitude'],row['Longitude']],
radius=15,
fill_color="#3db7e4",).add_to(m)
my_map = crime_data[['Latitude', 'Longitude']].values
m.add_child(plugins.HeatMap(my_map, radius=15))
m
我已经在您的 link 中下载了数据集,并设法获得了热图和 markercluster。然而,在这种情况下,Robbery 只取了数据的一个子集,只是为了检查代码是否有效,因为数据量很大。弹出窗口在数据框中 ARREST_PRECINCT。我提供了两种创建热图的方法,因为您正在使用这些方法。
import pandas as pd
import folium
from folium import plugins
from folium.plugins import HeatMap
from folium.plugins import MarkerCluster
nypd_data = pd.re ad_csv('NYPD_Arrest_Data__Year_to_Date_.csv')
nypd_robbery = nypd_data.loc[nypd_data['OFNS_DESC']=='ROBBERY']
my_heatmap = folium.Map(location=[40.695792, -73.939096], zoom_start=8)
heat_data = [[row['Latitude'],row['Longitude']] for index, row in nypd_robbery.iterrows()]
# Plot it on the map
HeatMap(heat_data).add_to(my_heatmap)
# Display the map
my_heatmap
#or
my_map = folium.Map(location=[40.695792, -73.939096], zoom_start=8)
robbery_loc = nypd_robbery[['Latitude', 'Longitude']].values
# plot heatmap
my_map.add_child(plugins.HeatMap(robbery_loc, radius=15))
my_map
#MarkerCluster
map_rob = folium.Map(location=[40.695792, -73.939096], zoom_start=10, tiles='Stamen Terrain')
marker_cluster = folium.plugins.MarkerCluster().add_to(map_rob)
for index,row in nypd_robbery.iterrows():
lat = row["Latitude"]
lon = row["Longitude"]
name = row["ARREST_PRECINCT"]
folium.Marker([lat,lon],popup=name).add_to(marker_cluster)
map_rob
在此处输入代码
我已经尝试将我的代码放入 Jupyter notebook 和 Google Colab 中,但是一旦我解决了代码中的所有错误,我不仅在输出中看到空白屏幕。当我 运行 在将热图数据集放在上面之前仅包含地图的代码时,它可以工作。我使用 https://alysivji.github.io/getting-started-with-folium.html 来构建我的代码。 这是数据集的 link: https://data.cityofnewyork.us/Public-Safety/NYPD-Arrest-Data-Year-to-Date-/uip8-fykc
我的完整代码如下。
here is what it looks like right now
import pandas as pd
pd.plotting.register_matplotlib_converters()
import matplotlib.pyplot as plt
%matplotlib inline
import seaborn as sns
import numpy as np
import folium
from folium import features
from folium import plugins
from folium.plugins import HeatMap
from folium.plugins import MarkerCluster
print("Setup Complete")
crime_filepath = "arrestdata.csv"
crime_data = pd.read_csv (crime_filepath, index_col = "ARREST_DATE")
crime_data.head()
#this is the section that doesn't work
m = folium.Map([40.7128, -74.0060], zoom_start=10.3)
m #this and the line above properly creats a blank map
for index, row in crime_data.iterrows():
folium.CircleMarker([row['Latitude'],row['Longitude']],
radius=15,
fill_color="#3db7e4",).add_to(m)
my_map = crime_data[['Latitude', 'Longitude']].values
m.add_child(plugins.HeatMap(my_map, radius=15))
m
我已经在您的 link 中下载了数据集,并设法获得了热图和 markercluster。然而,在这种情况下,Robbery 只取了数据的一个子集,只是为了检查代码是否有效,因为数据量很大。弹出窗口在数据框中 ARREST_PRECINCT。我提供了两种创建热图的方法,因为您正在使用这些方法。
import pandas as pd
import folium
from folium import plugins
from folium.plugins import HeatMap
from folium.plugins import MarkerCluster
nypd_data = pd.re ad_csv('NYPD_Arrest_Data__Year_to_Date_.csv')
nypd_robbery = nypd_data.loc[nypd_data['OFNS_DESC']=='ROBBERY']
my_heatmap = folium.Map(location=[40.695792, -73.939096], zoom_start=8)
heat_data = [[row['Latitude'],row['Longitude']] for index, row in nypd_robbery.iterrows()]
# Plot it on the map
HeatMap(heat_data).add_to(my_heatmap)
# Display the map
my_heatmap
#or
my_map = folium.Map(location=[40.695792, -73.939096], zoom_start=8)
robbery_loc = nypd_robbery[['Latitude', 'Longitude']].values
# plot heatmap
my_map.add_child(plugins.HeatMap(robbery_loc, radius=15))
my_map
#MarkerCluster
map_rob = folium.Map(location=[40.695792, -73.939096], zoom_start=10, tiles='Stamen Terrain')
marker_cluster = folium.plugins.MarkerCluster().add_to(map_rob)
for index,row in nypd_robbery.iterrows():
lat = row["Latitude"]
lon = row["Longitude"]
name = row["ARREST_PRECINCT"]
folium.Marker([lat,lon],popup=name).add_to(marker_cluster)
map_rob
在此处输入代码