使用 Plotly 的分类散点图

Categorical Scatter Plot with Plotly

我正在尝试用离散的数值 x 值绘制散点图。问题是 Plotly 将这些值解释为连续的,结果点的间距不均匀。在 Seaborn 中,我可以通过将 x 值转换为 str 来解决这个问题,但这在 Plotly 中不起作用。任何解决方案? MWE 下面:

4489058292    0.60
4600724046    0.26
6102975308    0.19
6122589624    0.10
4467367136    1.67
6008680375    2.50
4588967207    0.21
4941295226    0.34
4866979526    0.18
4906915418    0.38
test_df = pd.read_clipboard(sep="\s+", names=["ID", "Value"], index_col=0)

fig = px.scatter(
    test_df,
    x=test_df.index.astype(str),
    y=test_df,
)

fig.update_layout(showlegend=False)

IIUC,在 update_layout 中,您可以将 xaxis_type 指定为 category,例如:

fig = px.scatter(
    test_df,
    x=test_df.index, #no need of str here
    y=test_df,
)

fig.update_layout(showlegend=False, 
                  xaxis_type='category') #add this