3

概括

我想使用 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)
4

1 回答 1

2

您可以使用颜色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()

在此处输入图像描述

于 2020-04-04T16:36:16.390 回答