Search code examples
pythonplotplotly

Change graph color in Plotly, Python


Like we can change the color of the plotted graph in matplotlib using the color attribute, is there any way to change the graph's color in plotly?.


Solution

  • import plotly.graph_objects as go
    import numpy as np
    
    x = np.linspace(-np.pi, np.pi)
    y = np.cos(x)
    
    # this will create a blue line
    fig = go.Figure([
        go.Scatter(x=x, y=y)
    ])
    # to modify the figure after it has been created,
    # explore fig.data and target the appropriate attribute
    fig.data[0].line.color="red"
    fig