Continuous color scale for scatter traces added individually

Hi all, I’m furiously trying to create a line plot with several scatter traces added via add_scatter. Traces should be in “lines” mode. I want them to have a color based on value from numerical list (normalised_PDs), as in continuous color scale. I failed, even though I think it must be very simple. Could you please help me spot the problem here?

import plotly.express as px

PDs = [float(x) for x in"29.7498790938959 29.7498790938959 31.9080006487244 31.9080006487244 35.0832590944697 35.0832590944697 36.397623567387 53.904098545397 53.904098545397 55.6447697872446 55.6447697872446".split(" ")]
normalised_PDs = [(x - min(PDs)) / (max(PDs) - min(PDs)) for x in PDs]

fig_data2 = px.line(
    height=800,
    width=1600,
    title="Compiled HCT116 standard experiments in nano"
)

fig_data2 = fig_data2.update_layout(
    xaxis_title="Time, hours", 
    yaxis_title="Heat flow, W"
)

fig_data2 = fig_data2.update_traces(line=dict(width=2))

# Create a dictionary to map each number of cells to a color
color_map = {}

for experiment, ID, count, pd in zip(data_list, filtered_experiments, cell_numbers, normalised_PDs):
    fig_data2 = fig_data2.add_scatter(
        x=experiment.iloc[:, 1],
        y=experiment.iloc[:, 2],
        mode='lines',
        line=dict(width=4, color=color),
        name=str(int(count/1000)) + "k " + ID,
        marker_color=pd
    )

fig_data2 = fig_data2.update_xaxes(
    linewidth=2, 
    linecolor='black', 
    mirror=True, 
    ticks='inside', 
    showline=True, 
    gridcolor="white", 
    zerolinecolor="darkgray", 
    title_font=dict(size=40), 
    tickfont=dict(size=30),
    ticklen=20,
    tickwidth=4
)

fig_data2 = fig_data2.update_yaxes(
    linewidth=2, 
    linecolor='black', 
    mirror=True, 
    ticks='inside', 
    showline=True, 
    gridcolor="white", 
    zerolinecolor="darkgray", 
    title_font=dict(size=40), 
    tickfont=dict(size=30),
    ticklen=20,
    tickwidth=4
)

fig_data2 = fig_data2.update_layout(
    overwrite=True,
    plot_bgcolor="rgba(0, 0, 0, 0)",
    legend=dict(itemsizing='constant'),
    legend_font=dict(size=30),
    title_font=dict(size=50),
)

fig_data2.show()

1 Like

Hi @Madrigallo, if mode='lines you can only apply one color for the complete trace → you cant have a line with different colors based on anything.

See also

Hello, thanks for the answer. Maybe I didn’t explain well, the idea is that one trace (one line) is just one color, which is determined by the numerical value in normalised_PDs. I don’t need one trace to have different colors.
The question basically boils down to how to apply “continuous color” parameter when I’m adding traces individually, if I’m too lazy to convert the numerical value from normalised_PDs to rgb color.

I think you will have to come up with a custom function for that, I don’t see an other way.

named_colormap = 'viridis'
list_of_PD = [...]

def do(named_colormap, PD):
    ... magic happens here...
    return rgb_string

fig = go.Figure()

for value, PD in zip(values, list_of_PD):
    fig.add_scatter(x,y=values, mode='lines', line_color=do(PD)) 

Ok, thank you. I thought I could implement color scale directly for several traces with different numerical factors of PD, but I’m also fine with a workaround.

Can we alternatively add multiple traces at once (plotly.graph_objects.Figure — 5.18.0 documentation) while specifying a color scale, while addressing this desire, or are such features only available in the plotly express layers of the api?