在 Plotly Express 散点图中将所有标记设置为相同的固定大小

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

我正在寻找一种方法来将 Plotly Express 散点图中的所有标记设置为相同大小。
我想自己指定那个固定尺寸。

我知道您可以使用变量来设置标记的大小(使用 px.scatter(size='column_name'),但是它们会得到所有不同的大小。它们都需要具有相同的大小。

这是我的示例代码:

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', 
)

您可以按如下方式设置固定的自定义标记大小:

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

或者,您也可以创建一个额外的列,其中包含一个虚拟数值,并使用参数 size_max 来指定您想要给标记的大小:

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,
)