Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 225
Sander van den Oord Avatar asked Jan 13 '21 20:01

Sander van den Oord


People also ask

How do I change the size of the markers in Matplotlib?

To change the size of the markers, we use the s argument, for the scatter () function. This will be the markersize argument for the plot () function: We've also multiplied the value of each element in the list by an arbitrary number of 25, because they're ranked from 0..1.

How do I add a marker border to a scatter plot?

In order to make markers look more distinct, you can add a border to the markers. This can be achieved by adding the line property to the marker object. Here is an example of adding a marker border to a faceted scatter plot created using Plotly Express.

What is a data_frame in a scatter plot?

In a scatter plot, each row of data_frame is represented by a symbol mark in 2D space. data_frame ( DataFrame or array-like or dict) – This argument needs to be passed for column names (and not keyword names) to be used.

How can I set the size of markers in a column?

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: Thanks for contributing an answer to Stack Overflow!


Video Answer


1 Answers

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

like image 53
Sander van den Oord Avatar answered Nov 14 '22 06:11

Sander van den Oord