Set all markers to the same fixed size in Plotly Express scatterplot
Asked Answered
S

1

26

I'm looking for a way to set all markers in a Plotly Express scatter plot to the same size.
I want to specify that fixed size myself.

I know you can use a variable to set as size of the markers (with px.scatter(size='column_name'), but then they get all different sizes. They all need to have the same size.

Here's my sample code:

import pandas as pd
import plotly.express as px

df = pd.DataFrame({
    'colA': np.random.rand(10),
    'colB': np.random.rand(10),
})

fig = px.scatter(
    df, 
    x='colA', 
    y='colB', 
)

plotly express how to give markers all same fixed size

Stivers answered 13/1, 2021 at 20:22 Comment(0)
S
47

You can set a fixed custom marker size as follows:

fig.update_traces(marker={'size': 15})

Alternatively, you could also create an extra column with a dummy number value in it AND use argument size_max to specify the size you want to give your markers:

df['dummy_column_for_size'] = 1.

# argument size_max really determines the marker size!
px.scatter(
    df,
    x='colA', 
    y='colB', 
    size='dummy_column_for_size',
    size_max=15,
    width=500,
)

enter image description here

Stivers answered 13/1, 2021 at 20:22 Comment(2)
The size of the markers is scaled from the size_max value for the largest value in the size-column down to the smallest I suppose. My tests look like that ...Esmeraldaesmerelda
yes, that's why i create a 'dummy_column_for_size' with all value 1, so that they all have the same sizeStivers

© 2022 - 2024 — McMap. All rights reserved.