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',
)
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.
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.
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.
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!
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,
)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With