I try to get colors form colormaps by integer just like I would take a value from a list:
list[1]
The colormap I want to use can be found under "Qualitative ('tab20')" here:
CodePudding user response:
One potential drawback is that you may run of out colors before you run out of traces. A hacky way to solve this would be to just make the list repeat itself a bunch of times with:
col_list = px.colors.qualitative.Alphabet*10
Or you could use another approach from itertools
with cycle()
like this:
col_cycle = cycle(px.colors.qualitative.Alphabet)
fig = px.line(df, x=df.index, y=df.columns, template = 'plotly_dark')
fig.for_each_trace(lambda t: t.update(line_color = next(col_cycle)))
Complete code for count()
import pandas as pd
import numpy as np
import plotly.express as px
from itertools import cycle
from itertools import count
# data
np.random.seed(1)
df = pd.DataFrame(np.random.randint(-1,2,size=(100, 5)), columns=list('ABCDF'))
df.iloc[0] = 0
df = df.cumsum()
col_list = px.colors.qualitative.Alphabet*10
counter = count(start = 0, step = 1)
# figure
fig = px.line(df, x=df.index, y=df.columns, template = 'plotly_dark')
fig.for_each_trace(lambda t: t.update(line_color = col_list[next(counter)]))
fig.show()
Complete code for cycle()
import pandas as pd
import numpy as np
import plotly.express as px
from itertools import cycle
from itertools import count
# data
np.random.seed(1)
df = pd.DataFrame(np.random.randint(-1,2,size=(100, 5)), columns=list('ABCDF'))
df.iloc[0] = 0
df = df.cumsum()
# color cycle and figure
col_cycle = cycle(px.colors.qualitative.Alphabet)
fig = px.line(df, x=df.index, y=df.columns, template = 'plotly_dark')
fig.for_each_trace(lambda t: t.update(line_color = next(col_cycle)))
fig.show()