在 plotly 条形图中突出显示一个特定的条 [python]

Highlight one specific bar in plotly bar chart [python]

我正在使用 plotly.express 条形图来可视化分类数据,如下所示:

fig = px.bar(data, x='Algorithm', y='NDE', text_auto='.3f', color='Algorithm', 
color_discrete_map={'Ensembling':'orange', 'CO':'blue', 'Mean':'blue', 'DAE':'blue', 
                              'S2P':'blue', 'S2S':'blue', 'WinGru':'blue'})
fig.update_layout(barmode='stack', xaxis={'categoryorder':'total descending'})
fig.show()

给我这个结果:

但是,这种在图表中突出显示特定条形图的方式对我来说似乎有些“过度设计”。

所以我的问题是:有谁知道在 python plotly express 条形图中突出显示一个特定条形的更简单方法?

感谢任何建议! :-)

color_discrete_map 不需要那么明确。您可以定义默认颜色,然后覆盖。这仍然会生成与您的示例中相同类型的 color_discrete_map 字典,但它更健壮。

import plotly.express as px

default_color = "blue"
colors = {"China": "red"}

data = px.data.gapminder().query("year == 1952")

color_discrete_map = {
    c: colors.get(c, default_color) 
    for c in data.country.unique()}

fig = px.bar(data, x='country', y='pop', color='country',
             color_discrete_map=color_discrete_map)
fig.update_traces(showlegend=False)

fig.show()