Adding text overrides the symbol I want to use - px.scatter

Hello, Iโ€™m using px.scatter to create a scatter plot, and I want the symbols to be squares. Iโ€™d also like to show the text for each point.

Currently, Iโ€™m using

fig.update_traces(
    marker=dict(symbol="square",
                line=dict(width=2, color="DarkSlateGrey") ),
    selector=dict(mode="markers"),
)

to generate this chart:

Iโ€™d like to add text next to each square, and I have a corresponding columns in my dataframe that Iโ€™d like to use. However, when I enable the text parameter in px.scatter, the chart doesnโ€™t show squares anymore, and overrides to circles.

I.e. if I do:

fig = px.scatter(heatmap_data, 
                 x="...", 
                 y="...", 
                 size="...",
                 color = "...",
                 text = 'something',
                 color_continuous_scale= "...",
                 width=1100,
                 height=750,
                 title= "..."

The squares above become circles.

How can I avoid this, and add text without losing the squares?

1 Like

Try changing this to

selector=dict(mode="markers+text")

Example code:

import plotly.express as px
import numpy as np

x = np.random.randint(10, 50, 10)
y = np.random.randint(10, 50, 10)
text = np.random.randint(1, 3, 10)

fig = px.scatter(
    x=x,
    y=y,
    text=text
)

fig.update_traces(
    marker=dict(
        symbol="square",
        color='red',
        size=20,
        line=dict(width=2, color="DarkSlateGrey") ),
        selector=dict(mode="markers+text"),
)

Creates:

EDIT: you might even delete the selector parameter if you do wanted to change all traces in your figure.

1 Like

Thank you!

1 Like