[英]How to get variation of the color in choropleth Folium?
I am surely missing something in choropleth configuration.我肯定在 choropleth 配置中遗漏了一些东西。 Please find below code.请找到下面的代码。
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
PLease let me know what part of the choropleth is not correct?请让我知道 choropleth 的哪一部分不正确?
First of all, please use class folium.Choropleth()
instead of method choropleth()
which is deprecated.首先,请使用类folium.Choropleth()
而不是已弃用的方法choropleth()
。
For example, for your problem:例如,对于您的问题:
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)
Having said that, there are two problems in your code:话虽如此,您的代码中有两个问题:
key_on='name'
should be key_on='feature.properties.name'
根据geojson文件, key_on='name'
应该是key_on='feature.properties.name'
Neighborhood
in you DataFrame does not have names contained in the geojson file, therefore you are going to likely obtain a map like this:您 DataFrame 中的列Neighborhood
没有包含在 geojson 文件中的名称,因此您可能会获得这样的地图: In order to obtain a meaningful choropleth map, names in sf_neighborhood_geo
should correspond to values in sf['Neighborhood']
.为了获得有意义的区域分布图, sf_neighborhood_geo 中的名称sf_neighborhood_geo
应于sf['Neighborhood']
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.