使用plotly python在多条线图中添加色阶

时间:2019-07-11 14:29:14

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

摘要

我想使用plotly-python(plotly == 3.7.1)将色标添加到多条线图中。

  • 我不想手动声明每个颜色的十六进制。
  • 年份应该订购颜色图(例如:2000是柔和的蓝色... 2018是深蓝色的)

当前情节

series with default colors

示例图

series with colormap

代码

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

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

1 个答案:

答案 0 :(得分:1)

您可以使用colour库中的range_to()函数在两种不同的颜色(例如,浅蓝色和深蓝色)之间生成一系列颜色。

import numpy as np
import plotly.graph_objects as go
from colour import Color

# number of lines
N = 10 

start_color = "#b2d8ff" # light blue
end_color = "#00264c" # 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(showlegend=True, 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):

    # artificial data
    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()

enter image description here