How to set custom color to a selected bar?

Hey guys,

I would like to change the colors of my bars when I click on a specific color and so, set the selected bar as red and all the unselected ones as white; I didn’t find references about doing this, so I would appreciate a lot any help on changing it;

I have added a reproducible code that can be used

import plotly.express as px
import pandas as pd

df = pd.DataFrame({
    "x": ["val1", "val2", "val3","val4", "val5", "val6"],
    "y": [4,6,8,4, 7,7],
    "fruit": ["apple", "apple", "orange", "orange", "orange", "apple"]
})

fig = px.bar(df, x="x", y="y", custom_data=["fruit"])

fig.update_layout(barmode="group", clickmode="event+select")

The wrong output:

Thank you very much in advance for any collaboration;
Regards,
Leonardo

Hi Leonardo,

Please take a look here.

1 Like

Hey, after posting here I did find a solution using exactly it

I will paste what I did below:

import plotly.express as px
import pandas as pd

df = pd.DataFrame({
    "x": ["val1", "val2", "val3","val4", "val5", "val6"],
    "y": [4,6,8,4, 7,7],
    "fruit": ["apple", "apple", "orange", "orange", "orange", "apple"]
})

fig = px.bar(df, x="x", y="y", custom_data=["fruit"])

fig.update_layout(barmode="group", clickmode="event+select")

for figure in fig.data:
    figure.update(
        selected=dict(marker=dict(color="white")),
        unselected=dict(marker=dict(color="black", opacity=0.75)),
    )

Anyway, thank you very much for your answer!

Regards,
Leonardo