Search code examples
pythonplotly

Python: How do I change the colors of lines on a multi-line chart in plotly?


How do I change the colors of the lines in the plotly chart below? Thanks

import plotly.express as px

# forecast_eval is a dataframe with an actual number, a forecast 
# number, and upper and lower forecast bounds

# Draw a line chart using 4 columns forecast_eval.columns[2:6]
eval_line = px.line(forecast_eval, x='ds', y=forecast_eval.columns[2:6], 
title='Forecast')

eval_line

Line chart


Solution

    • you have not provided sample data, have simulated a dataframe of same structure implied by sample code
    • simple case of using color_discrete_sequence
    import plotly.express as px
    import numpy as np
    import pandas as pd
    
    # forecast_eval is a dataframe with an actual number, a forecast
    # number, and upper and lower forecast bounds
    
    forecast_eval = pd.concat(
        [
            pd.DataFrame({"ds": np.linspace(0, 100, 1000)}),
            pd.DataFrame(
                np.random.uniform(np.linspace(0, 10**4, 5), np.linspace(1, 2*10**4, 5), [1000, 5])
            ),
        ],
        axis=1,
    )
    
    # Draw a line chart using 4 columns forecast_eval.columns[2:6]
    eval_line = px.line(
        forecast_eval,
        x="ds",
        y=forecast_eval.columns[2:6],
        title="Forecast",
        color_discrete_sequence=["yellow", "blue", "pink", "skyblue"],
    )
    
    eval_line
    

    enter image description here