Plotly graphics_object determine discrete colors to be used

Hi All,

I am new to plotly and dash, so this is my first question.

I have a dataframe with coordinates, each coordinate couple (E, N, U) have a quality level which is an integer ranging between 1 (best) and 7 (worst). I want to assign the colors to each quality (1: green, 2: yellow, … 7: red). Since these coordinates are represented on 3 subplots, so I work with ‘make_subplots’ adding traces for each coordinate versus datatime using the go.Scatter function.

When using plotly.express, I was able to set the color on a single plot using

color=‘Q’, color_discrete_map=q_plotly[‘color’]

where the latter is a dict linking the quality to the desired color. For this to work I had to change the Quality in the dataframe from int to str.

How could I get a similar result using the go.Scatter?

Tx/Alain

Hi,

Welcome to the community! :slightly_smiling_face:

Assuming the quality is in a column “Q” of a dataframe df, and q_plotly={1: "green", ...} maps the values in Q to color names, then you can do:

go.Scatter(
    x=df[...],
    y=df[...],
    marker={
        "color": df["Q"].apply(lambda x: q_plotly[x])
    }
   # or marker_color = df["Q"]...., if you don't want to pass a full dict
) 

In other words, you can specify individual colors by passing an iterable (list or pd.Series) to marker.color, as well as to other marker properties (like symbol or size).

Lastly, you can also consider using “facet_row” in plotly express and provide the column having the three “coordinates” E, N, U, if those are the 3 subplots you want to create (instead of using make_subplot).