如何获得鹅卵石颜色的变化?

问题描述 投票:0回答:1

我肯定是在Choropleth配置中缺少了一些东西。请找到下面的代码。

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

请让我知道choropleth的哪个部分不正确?

python python-3.x pandas folium choropleth
1个回答
0
投票

您的代码中有两个问题:

  1. 根据geojson文件,key_on='name'应该为key_on='feature.properties.name'
  2. DataFrame中的Neighborhood列没有包含在geojson文件中的名称,因此您很可能会获得像这样的地图:

enter image description here

为了获得有意义的Choropleth映射,geo_data中的名称与data中的名称(sf_map.choropleth()内的名称之间的交集必须不为空。

© www.soinside.com 2019 - 2024. All rights reserved.