Search code examples
plotlyplotly-dashplotly-python

Plotly-Dash line chart: custom color not working properly


I have a plot in which I want to have different color for different columns based on some logic.

col_dict = get_color_map(df.columns)
fig = px.line(data_frame=df, x=df.index, y=df.columns, color_discrete_map=col_dict)

The get_color_map function returns a dict with column names as keys as string "hsv(h, s, v)" as value.

The legends and hover data color are correct but the lines itself are just all white. I want the color of the lines to match the legend. enter image description here

Note: I tried using the color_discrete_sequence parameter as will, I got the same result only.


Solution

  • I think plotly express does not provide custom color map without specifying the color parameter, which in data frame case should be a column itself. Ref

    So in this case where each column by itself should have different color without a hue column, it is best to use the plotly graph_object library.

    The following code works:

    fig = go.Figure()
    for col in df.columns:
        fig.add_trace(go.Scatter(x=df.index, y=df[col], name=col, mode='lines', line=dict(color=col_dict[col])))