具有散点图的分类散点图

时间:2020-05-18 11:41:15

标签: 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
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)

1 个答案:

答案 0 :(得分:4)

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