如何获得 choropleth Folium 的颜色变化?
How to get variation of the color in choropleth Folium?
我肯定在分区表配置中遗漏了一些东西。请找到以下代码。
import pandas as pd
import folium
df = pd.read_csv("https://cocl.us/sanfran_crime_dataset",index_col=0)
# group by neighborhood
sf = df.groupby('PdDistrict').count()
sf = pd.DataFrame(sf,columns=['Category']) # remove unneeded columns
sf.reset_index(inplace=True) # default index, otherwise groupby column becomes index
sf.rename(columns={'PdDistrict':'Neighborhood','Category':'Count'}, inplace=True)
sf.sort_values(by='Count', inplace=True, ascending=False)
sf
# San Francisco latitude and longitude values
latitude = 37.77
longitude = -122.42
sf_neighborhood_geo = 'https://raw.githubusercontent.com/codeforamerica/click_that_hood/master/public/data/san-francisco.geojson'
# Create map
sf_map = folium.Map(location=[latitude,longitude], zoom_start=12)
# Use json file TEST based on class
sf_map.choropleth(
geo_data=sf_neighborhood_geo,
data=sf,
columns=['Neighborhood','Count'],
key_on='name',
fill_color='YlOrRd',
fill_opacity='0.7',
line_opacity='0.3',
legend_name='Crime Rate in San Francisco, by Neighborhood')
folium.LayerControl().add_to(sf_map)
# display the map
sf_map
请告诉我哪个部分不正确?
首先,请使用已弃用的class folium.Choropleth()
instead of method choropleth()
。
例如,对于你的问题:
m = folium.Map(location=[latitude,longitude], zoom_start=12)
folium.Choropleth(geo_data=sf_neighborhood_geo,
name='choropleth',
data=sf,
columns=['Neighborhood','Count'],
key_on='feature.properties.name',
fill_color='YlOrRd',
fill_opacity=0.7,
line_opacity=0.2,
legend_name='Crime Rate in San Francisco, by Neighborhood').add_to(m)
folium.LayerControl().add_to(m)
话说回来,你的代码有两个问题:
- 根据geojson文件,
key_on='name'
应该是key_on='feature.properties.name'
- 您 DataFrame 中的列
Neighborhood
没有包含在 geojson 文件中的名称,因此您可能会获得这样的地图:
为了获得有意义的等值线图,sf_neighborhood_geo
中的名称应对应于 sf['Neighborhood']
中的值。
我肯定在分区表配置中遗漏了一些东西。请找到以下代码。
import pandas as pd
import folium
df = pd.read_csv("https://cocl.us/sanfran_crime_dataset",index_col=0)
# group by neighborhood
sf = df.groupby('PdDistrict').count()
sf = pd.DataFrame(sf,columns=['Category']) # remove unneeded columns
sf.reset_index(inplace=True) # default index, otherwise groupby column becomes index
sf.rename(columns={'PdDistrict':'Neighborhood','Category':'Count'}, inplace=True)
sf.sort_values(by='Count', inplace=True, ascending=False)
sf
# San Francisco latitude and longitude values
latitude = 37.77
longitude = -122.42
sf_neighborhood_geo = 'https://raw.githubusercontent.com/codeforamerica/click_that_hood/master/public/data/san-francisco.geojson'
# Create map
sf_map = folium.Map(location=[latitude,longitude], zoom_start=12)
# Use json file TEST based on class
sf_map.choropleth(
geo_data=sf_neighborhood_geo,
data=sf,
columns=['Neighborhood','Count'],
key_on='name',
fill_color='YlOrRd',
fill_opacity='0.7',
line_opacity='0.3',
legend_name='Crime Rate in San Francisco, by Neighborhood')
folium.LayerControl().add_to(sf_map)
# display the map
sf_map
请告诉我哪个部分不正确?
首先,请使用已弃用的class folium.Choropleth()
instead of method choropleth()
。
例如,对于你的问题:
m = folium.Map(location=[latitude,longitude], zoom_start=12)
folium.Choropleth(geo_data=sf_neighborhood_geo,
name='choropleth',
data=sf,
columns=['Neighborhood','Count'],
key_on='feature.properties.name',
fill_color='YlOrRd',
fill_opacity=0.7,
line_opacity=0.2,
legend_name='Crime Rate in San Francisco, by Neighborhood').add_to(m)
folium.LayerControl().add_to(m)
话说回来,你的代码有两个问题:
- 根据geojson文件,
key_on='name'
应该是key_on='feature.properties.name'
- 您 DataFrame 中的列
Neighborhood
没有包含在 geojson 文件中的名称,因此您可能会获得这样的地图:
为了获得有意义的等值线图,sf_neighborhood_geo
中的名称应对应于 sf['Neighborhood']
中的值。