Dynamically change color_by column

Hey :slight_smile:

I have a table with multiple columns as metadata (categorical) and one column for a value
lets say the columns look something like this:
ID, value, meta1,meta2,meta3

I have a box plot

fig = px.box(df, x='ID', y='value', color='meta1', template="plotly_white")

I would like to add a dropdown to dynamically change the meta1 column i set for the color attribute
is it possible?
I have tried:

    fig.update_layout(updatemenus=[
        dict(
            buttons=list([
                dict(
                      args=["color", "meta1"],
                      label="meta1",
                      method="restyle"
                      ),
                dict(
                    args=["color", "meta2"],
                    label="meta2",
                    method="restyle"
                )

but no luckโ€ฆ
any suggestion how can i do this?

Hi,
Iยดve the same problem.
Did you find any solution?

best regards!

I found a solution by using either streamlit with plotly or Holoviz Panel with plotly and linking the color parameter to the widgets there
with streamlit it is very easy, if you need help let me know and I will write a sample code

hi @joker
:wave: Welcome to the community.

Are you referring to something like this:

import plotly.express as px
from dash import Dash, dcc, html, Input, Output, callback

df = px.data.tips()
print(df.columns)

app = Dash(__name__)
app.layout = html.Div([
    dcc.Dropdown(['sex','smoker','time'], 'smoker', id='col-selected'),
    dcc.Graph(id='box-plot')
])

@callback(
    Output('box-plot','figure'),
    Input('col-selected','value')
)
def update_graph(column_selected):
    fig = px.box(df, x="day", y="total_bill", color=column_selected, template="plotly_white")
    return fig

if __name__ == '__main__':
    app.run_server(debug=True)
2 Likes