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

标签 python colors plotly plotly-python

对于 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
                   ) 

绘图

enter image description here

完整代码:

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()

关于python - Plotly - 从颜色图中设置颜色循环,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/71205815/

相关文章:

python - 在 3D numpy 对象中查找与给定 z 值相对应的 x 和 y 值

button - 如何在odoo中更改按钮的颜色并使其在单击一次后禁用?

java - 和引擎库上的颜色设置

python - JupyterLab 中 Plotly 的 Right 扩展名是什么?

javascript - 使用plotly.js 对单个数据进行多个Y 轴图例

python - 将一些列 append 到列表矩阵的列表中

python - 在 Python 中比较 2 个巨大的 csv 文件

javascript - 如何在 React 中绘制红色水平线

r - 不使用 Shiny 的链接图

python - 空的 __init__.py 文件如何正确?