How to Change Plotly Express bar colors using the Partial Property Update Patch() in Plotly Dash

24 Views Asked by At

I have plot in dash plotly set like this

graph = px.bar(df, x=df[default_x], y=df[default_y], color=df[defualt_color], orientation= "h",)

and change data in patch in the callback as follow

def generate_colors(strings):
    unique_strings = list(set(strings))
    num_unique = len(unique_strings)
    colors = np.linspace(0, 1, num_unique)  # You can choose any colormap you like
    color_dict = {}

    for i, string in enumerate(unique_strings):
        color_dict[string] = colors[i]

    return color_dict

def assign_colors(strings, color_dict):
    colors = []
    for string in strings:
        colors.append(color_dict[string])
    return colors
@app.callback(
    Output('graph', 'figure'),
    Input('x', 'value'),
    Input('y', 'value'),
    Input('color','value')
    )
def graph_change(x, y,color):
    patch_figure = Patch()
    patch_figure ["data"][0]["x"] = df[x]
    patch_figure ["data"][0]["y"] = df[y]
    color_dict = generate_colors(df[color])
    colors = assign_colors(df[color], color_dict)
    patch_figure ['data'][0]['name'] = df[color]
    patch_figure ["data"][0]['line']['color'] = colors
    # patch code to dynamically update figure color input based on

    return patch_figure

but the color and legend are not changing, how can I make it work?

0

There are 0 best solutions below