Line or marker color repetition if more than 10 traces

If there are more than 10 traces during making Scatter chart, the color of the lines or spots will repeat, which makes distinguishing difficult.
any way to revise it?

FYI, below code and output as an expample:

fig = go.Figure()

    for crop_year, group in df_fig.groupby('Crop_Year'):
        fig.add_trace(
            go.Scatter(x=group['Value'], y=group['CLOSE'], marker=dict(size=10, line=dict(width=2)),
                       name=crop_year)
        )

Hi @michaelyang,

You can change the default color palette to a different pallet with more individual colors, for example alphabet contains 26 colors

An example:

import pandas as pd
import plotly.express as px
import numpy as np
import string


df = pd.DataFrame(
    np.random.randint(1,20,size=(26,26)), 
    columns=list(string.ascii_uppercase)
)
fig = px.scatter(
    df, 
    x=df.index,
    y=df.columns,
    color_discrete_sequence=px.colors.qualitative.Alphabet
)
fig.show()


mrep colors

1 Like

yes, got it. it works perfectly when drawing fig with px function. thanks very much!
what if drawing the fig with add_trace() function like my example?

You can specify the colorway in the layout:

import plotly.graph_objects as go
import numpy as np

fig = go.Figure(layout={'height':600, 'width':600, 'colorway':px.colors.qualitative.Alphabet})

for _ in range(26):
    fig.add_scatter(
        x=np.arange(10),
        y=np.random.randint(1, 20, size=(10)),
        mode='markers'
)
fig.show()

or change it afterwards:

fig.update_layout(colorway=px.colors.qualitative.Alphabet)

that is perfectly solved, tks very much!

1 Like