Sander van den Oord
Sander van den Oord

Reputation: 12838

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

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

Upvotes: 27

Views: 36700

Answers (1)

Sander van den Oord
Sander van den Oord

Reputation: 12838

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

Upvotes: 47

Related Questions