【问题标题】:Categorical Scatter Plot with Plotly带有 Plotly 的分类散点图
【发布时间】:2020-09-04 04:51:24
【问题描述】:

我正在尝试使用离散的数字 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)

【问题讨论】:

  • 预期输出只是其中 x 值被视为分类的输出,以便点均匀分布。稍后我会添加预期的输出。

标签: python pandas plotly-express


【解决方案1】:

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

【讨论】:

  • 太棒了!这正是我一直在寻找的。谢谢!
猜你喜欢
  • 2021-05-11
  • 2020-11-06
  • 1970-01-01
  • 2021-11-04
  • 1970-01-01
  • 2020-05-15
  • 1970-01-01
  • 2017-11-03
  • 1970-01-01
相关资源
最近更新 更多