我想使用plotly-python(plotly == 3.7.1)将色标添加到多条线图中。
layout = go.Layout(
title = '',
showlegend = True,
xaxis = dict(
title = '',
zeroline = False
),
yaxis = dict(
title = '',
zeroline = False,
)
)
fig = go.Figure(data = data, layout = layout)
答案 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()