【问题标题】:Plotly: Add colorscale in multiple lines plot using PythonPlotly:使用 Python 在多线图中添加色阶
【发布时间】:2019-11-21 07:29:44
【问题描述】:

总结

我想使用 plotly-python (plotly==3.7.1) 为多个折线图添加色阶。

  • 我不想手动声明每种颜色的十六进制。
  • 颜色图应按年份排序(例如:2000 年为浅蓝色 ... 2018 年为深蓝色)

目前正在绘制

示例图

代码

layout = go.Layout(
        title              = '',
        showlegend         = True,
        xaxis = dict(
            title          = '',
            zeroline       = False
        ),
        yaxis = dict(
            title          = '',
            zeroline       = False,
        )
    )    

fig = go.Figure(data = data, layout = layout)

【问题讨论】:

    标签: python time-series plotly timeserieschart plotly-python


    【解决方案1】:

    您可以使用colour 库中的range_to() 函数来生成色标,例如在浅蓝色和深蓝色之间:

    import numpy as np
    import plotly.graph_objects as go
    from colour import Color
    
    N = 10                   # Number of lines
    start_color = '#b2d8ff'  # Start color (light blue)
    end_color = '#00264c'    # End color (dark blue)
    
    # List of N colors between start_color and end_color
    colorscale = [x.hex for x in list(Color(start_color).range_to(Color(end_color), N))]
    
    layout = dict(
        plot_bgcolor='white',
        margin=dict(l=0, r=0, t=0, b=0),
        xaxis=dict(zeroline=False, showgrid=False, mirror=True, linecolor='#d9d9d9'),
        yaxis=dict(zeroline=False, showgrid=False, mirror=True, linecolor='#d9d9d9')
    )
    
    data = []
    
    for i in range(N):
    
        x = np.linspace(0, 3)
        y = i + np.exp(x)
    
        data.append(
            go.Scatter(
                x=x,
                y=y,
                mode='lines',
                line=dict(color=colorscale[i], width=2),
                name='Line ' + str(i + 1)
            )
        )
    
    fig = go.Figure(data=data, layout=layout)
    
    fig.show()
    
    

    【讨论】:

      猜你喜欢
      • 2019-03-05
      • 1970-01-01
      • 1970-01-01
      • 2020-05-22
      • 1970-01-01
      • 1970-01-01
      • 2022-07-13
      • 2020-05-28
      • 1970-01-01
      相关资源
      最近更新 更多