Plotly: How to set line color?

前端 未结 3 1429
时光取名叫无心
时光取名叫无心 2021-02-20 00:30

How can I set the color of a line in plotly?

import plotly.graph_objects as go
from plotly.subplots import make_subplots


fig = make_subplots(rows=         


        
3条回答
  •  名媛妹妹
    2021-02-20 01:16

    @nicolaskruchten is of course right, but I'd like to throw in two other options:

    line_color="#0000ff"
    

    and

     fig['data'][0]['line']['color']="#00ff00"
    

    I particularly appreciate the flexibility of the latter option since it easily lets you set a new color for a desired line after you've built a figure using for example fig.append_trace(go.Scatter()) or fig = go.Figure(data=go.Scatter)). Below is an example using all three options.

    Code 1:

    import plotly.graph_objects as go
    import numpy as np
    
    t = np.linspace(0, 10, 100)
    y = np.cos(t)
    y2= np.sin(t)
    fig = go.Figure(data=go.Scatter(x=t, y=y,mode='lines+markers', line_color='#ffe476'))
    fig.add_trace(go.Scatter(x=t, y=y2,mode='lines+markers', line=dict(color="#0000ff")))
    fig.show()
    

    Plot 1:

    Now you can change the colors directly if you insert the snippet below in a new cell and run it.

    Code 2:

    fig['data'][0]['line']['color']="#00ff00"
    fig.show()
    

    Plot 2:

提交回复
热议问题