使用 Plotly 绘制分类散点图

K G*_*K G 4 python pandas plotly-express

我正在尝试绘制具有离散数值 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
Run Code Online (Sandbox Code Playgroud)
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)
Run Code Online (Sandbox Code Playgroud)

Ben*_*n.T 5

IIUC,在 中update_layout,您可以将 指定xaxis_typecategory

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
Run Code Online (Sandbox Code Playgroud)