Choropleth贴图无颜色区分

2024-09-30 16:38:44 发布

您现在位置:Python中文网/ 问答频道 /正文

我正试图创建一个基于多伦多社区负担不起的住房率的choropleth地图。我在网上找到了一个数据集,我在csv中读取该数据集,然后对数据进行条件处理以生成适当的列,然后尝试创建我的地图

我的数据和条件代码如下所示:

df = pd.read_csv('torontodata.csv')
df = df.transpose()
df.columns = df.iloc[4]
df.drop(['_id' , 'Category', 'Topic', 'Data Source', 'Characteristic'], axis=0, inplace=True)
df = df.reset_index()
df = pd.DataFrame(df[['index', 'Rate of unaffordable housing']])
df.drop(df.loc[df['index']=='City of Toronto'].index, inplace=True)
df['Rate of unaffordable housing'] = df['Rate of unaffordable housing'].astype(float)

我很有信心这是正确的,因为它返回的结果与它应该返回的完全一样: dataframe。我认为错误一定是在choropleth映射的“key_on”参数中,但我不知道我做错了什么。我检查了原始geojson文件,似乎找到了正确的路径

!wget --quiet https://raw.githubusercontent.com/codeforamerica/click_that_hood/master/public/data/toronto.geojson

print('GeoJSON file downloaded!')
toronto_geo = r'toronto_crs84.geojson' # geojson file

address = 'Toronto, ON'

geolocator = Nominatim(user_agent="foursquare_agent")
location = geolocator.geocode(address)
latitude = location.latitude
longitude = location.longitude
print(latitude, longitude)

toronto_map = folium.Map(location=[latitude, longitude], zoom_start=13) # generate map centred around Toronto
threshold_scale = np.linspace(df['Rate of unaffordable housing'].min(),
                              df['Rate of unaffordable housing'].max(),
                              6, dtype=int)
threshold_scale = threshold_scale.tolist() # change the numpy array to a list
threshold_scale[-1] = threshold_scale[-1] + 1 # make sure that the last value of the list is greater than the maximum immigration


# display map

toronto_map.choropleth(
    geo_data=toronto_geo,
    data=df,
    columns=['index', 'Rate of unaffordable housing'],    
    key_on='feature.properties.name', 
    threshold_scale=threshold_scale, 
    fill_color='YlOrRd', 
    fill_opacity=0.3, 
    line_opacity=0.2,
    legend_name='Affordable housing in Canada'
)
toronto_map

这不会返回错误,但我的贴图都是一个颜色值。我在这个问题上纠缠了一整天。我甚至尝试使用不同的json文件,但我遇到了同样的问题。任何帮助都将不胜感激

https://raw.githubusercontent.com/codeforamerica/click_that_hood/master/public/data/toronto.geojson


Tags: of数据mapdfdataindexthresholdrate