【问题标题】:How to show every value in y-axis using Plotly Express?如何使用 Plotly Express 在 y 轴上显示每个值?
【发布时间】:2021-11-14 19:21:23
【问题描述】:

当我这样做时:

fig = px.line(df, x="day", y="avg_spending")


fig.show()

它不会将 y 轴上的值乘以 2(0、2、4、...)。我希望它是 1 乘 1(0,1,2,3,..)。我在 df 中“avg_spending”的最大值是 17,所以我希望 y 轴上有 1,2,3,...,17。该怎么做?

【问题讨论】:

    标签: python python-3.x plotly plotly-express


    【解决方案1】:

    在轴上设置dtick参数

    import numpy as np
    import pandas as pd
    import plotly.express as px
    
    df = pd.DataFrame({"day":range(50),"avg_spending":np.random.randint(1,17,50)})
    
    fig = px.line(df, x="day", y="avg_spending")
    fig.update_layout(yaxis={"dtick":1},margin={"t":0,"b":0},height=500)
    
    
    

    【讨论】:

    • 谢谢,但它们都相互覆盖。如何在刻度之间保持空间?
    • 已更新 - 只需给它更多空间 ....
    • 是否可以添加0作为轴的起点?因为我现在有一个吗?
    • 看看文档,如果你想在yaxis范围内包含零,你可以配置很多东西plotly.com/python/reference/layout/xaxisfig.update_layout(yaxis={"dtick":1,"range":[0,17]},margin={"t":0,"b":0},height=500)
    猜你喜欢
    • 2020-11-01
    • 2020-04-26
    • 2021-03-22
    • 2022-11-29
    • 2020-01-28
    • 1970-01-01
    • 1970-01-01
    • 2019-01-24
    • 2023-02-04
    相关资源
    最近更新 更多