Plotly - 从颜色图中设置颜色循环

Plotly - color cycle setting from a colormap

对于 matplotlib,我使用这段代码更改了默认的颜色循环设置,这样我就可以在这个循环中用颜色绘制多条线。


n = 24
color = plt.cm.viridis(np.linspace(0, 1,n))
mpl.rcParams['axes.prop_cycle'] = cycler.cycler('color', color) 

for i in range(30):
    plt.plot(x,y,data[data["col1"]==i]) 
plt.show()

我怎样才能在 plotly 中做到这一点?

fig = px.line(data[data["col1"]==i],x,y)

for i in range(30):
    fig.add_scatter(x,y,data[data["col1"]==i],mode="line") 

我经常使用 from itertools import cyclenext(<list of colors>),其中 <list of colors> 可以是任何颜色序列,例如 px.colors.qualitative.Alphabet.

这是一个接近您正在寻找的设置:

fig = go.Figure()
for i in range(lines):
    color =  next(col_cycle)
    fig.add_scatter(x = np.arange(0,rows),
                    y = np.random.randint(-5, 6, size=rows).cumsum(),
                    mode="lines",
                    line_color = color,
                    name = color
                   ) 

情节

完整代码:

from itertools import cycle
import numpy as np
col_cycle = cycle(px.colors.qualitative.Alphabet)

rows = 10
lines = 30

fig = go.Figure()
for i in range(lines):
    color =  next(col_cycle)
    fig.add_scatter(x = np.arange(0,rows),
                    y = np.random.randint(-5, 6, size=rows).cumsum(),
                    mode="lines",
                    line_color = color,
                    name = color
                   ) 
fig.show()