[英]how to highlight a single bar on select in Plotly Dash
我正在使用 Plotly Dash 构建仪表板。 我使用 slider 作为条形图的输入。 我想使用下拉菜单在条形图中突出显示单个国家/地区。 连接如图中的黄色箭头所示。
我已经使用 'fig1.for_each_trace' 将其连接到下拉列表。 它没有给出错误,但不是我想要的。
谁能帮我吗?
这个链接让我朝着这个方向前进: https://plotly.com/python/creating-and-updating-figures/#conditionally-updating-traces
到目前为止我的回调:
'''
@app.callback(
[
Output('barchart-1', 'figure'),
Output('year-1', 'children')
],
Input('year-slider', 'value'),
Input('dropdown-country', 'value')
)
def update_visual(slctd_year, slctd_cntry):
life = df1[df1['Year'] == slctd_year]
country = df1[df1['Entity'] == slctd_cntry]
fig1 = px.bar(
life,
x=life['Entity'],
y=life['Life expectancy'],
# template='simple white'
)
fig1.update_traces(marker_color='rgb(33, 60, 99)',
)
fig1.update_layout(
height= 315,
margin=dict(l=20, r=30, t=50, b=30),
plot_bgcolor='rgb(0,0,0,0)',
legend_bgcolor='rgba(0,0,0,0)',
paper_bgcolor='rgb(0,0,0,0)',
font_color='#909090'
)
fig1.for_each_trace(
lambda trace: trace.update(marker_color='#9D3469') if trace == country else (),
)
return fig1, 'Year: {0}'.format(slctd_year)
'''
在 Plotly 社区论坛上,我得到了这个答案,它工作得很好!
'''
fig1["data"][0]["marker"]["color"] = ["red" if c == slctd_cntry else "blue" for c in fig1["data"][0]["x]" ]
'''
这是该主题的链接: https://community.plotly.com/t/how-to-highlight-a-single-bar-on-select-in-plotly-dash/60739/2
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.