Learn how to use Dash Bio for next-gen sequencing & quality control. 🧬Register for the Oct 27 webinar.

Not able to colour markers based on discrete categorical data

Hi

I am struggling to colour my scatter markers based on categorical values rather than a continuous colour scale.

This produces a continuous colour scale and the chart loads:

data = go.Scatter(
        x=df_searches['price'],
        y=df_searches['variance'],
        name='value_vs_position',
        mode='markers',
        marker = dict(
            size = 10,
            color = df_searches['search_position'],
            showscale=True
        ),
        text=df_searches['reg']
    )

But the following, as suggested in docs (Discrete Colors | Python | Plotly), gives me an error:

df_searches['search_position'] = df_searches['search_position'].astype(str)

    data = go.Scatter(
        x=df_searches['price'],
        y=df_searches['variance'],
        name='value_vs_position',
        mode='markers',
        marker = dict(
            size = 10,
            color = df_searches['search_position'],
            showscale=True
        ),
        text=df_searches['reg']
    )

Error message …

Invalid element(s) received for the ‘color’ property of scatter.marker
Invalid elements include: [‘2’, ‘2’, ‘2’, ‘2’, ‘2’, ‘2’, ‘2’, ‘2’, ‘2’, ‘2’]

The 'color' property is a color and may be specified as:
  - A hex string (e.g. '#ff0000')
  - An rgb/rgba string (e.g. 'rgb(255,0,0)')
  - An hsl/hsla string (e.g. 'hsl(0,100%,50%)')
  - An hsv/hsva string (e.g. 'hsv(0,100%,100%)')
  - A named CSS color:
        aliceblue, antiquewhite, aqua, aquamarine, azure,
        beige, bisque, black, bla

The docs that suggested converting the df column to string does relate to Plotly Express, so perhaps that is another method for Dash?

TIA

I think this is only possible for px.scatter… If you want to it in an graph object do it like this:
color= df_searches[‘search_position’],
cmin = df_searches[‘search_position’].min(),
cmax= df_searches[‘search_position’].max(),
colorscale = ‘Viridis’,
colorbar=dict(
title= ‘title’
),

Chris, I think you can solve this one by referencing the color field’s string name rather than the color field object itself, like:

color = 'search_position'

1 Like