对于 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 cycle
和next(<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()
关于python - Plotly - 从颜色图中设置颜色循环,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/71205815/