我正在使用Python和Dash创建一个web应用程序,并使用ISO_ALPHA值创建了一个choropleth地图。
使用回调,我已经过滤了数据,这当然会更新地图,它工作得很好。但是,我想添加一个颜色尺度,而不是基于单元格值,我希望它是基于每个国家的行数。这是为了显示每个国家有多少玩家。
代码
#call back to update map
@app.callback(
Output(component_id='player_map', component_property='figure'),
[Input(component_id='select_list', component_property='value')]
)
def display_map(list_selected):
dff_map = df_map[df_map.band==list_selected]
fig = px.choropleth(dff_map, locations='iso_alpha', hover_name='iso_alpha')
fig.update_geos(fitbounds="locations")
return fig提前谢谢你

发布于 2021-05-28 02:07:06
不确定是否有更有效的方法,但设法找到了一个解决方案(见下文),它将创建一个新的列“count”,并根据国家代码的计数添加计数值。
def display_map(list_selected):
dff_map = df_map[df_map.band==list_selected]
# make a dict with counts
count_dict = {d:(dff_map['iso_alpha']==d).sum() for d in dff_map.iso_alpha.unique()}
# assign that dict to a column
dff_map['count'] = [count_dict[d] for d in dff_map.iso_alpha]
fig = px.choropleth(dff_map, locations='iso_alpha', hover_name='iso_alpha', color='count')
fig.update_geos(fitbounds="locations")
return fighttps://stackoverflow.com/questions/67727372
复制相似问题