18

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

Sander van den Oord
  • 10,986
  • 5
  • 51
  • 96

1 Answers1

33

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

Sander van den Oord
  • 10,986
  • 5
  • 51
  • 96
  • 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 ... – flipSTAR Apr 07 '22 at 12:32
  • yes, that's why i create a 'dummy_column_for_size' with all value 1, so that they all have the same size – Sander van den Oord Apr 07 '22 at 20:39