【问题标题】:plotly is labeling my xaxis wrong. how to fix this?plotly 将我的 xaxis 标记为错误。如何解决这个问题?
【发布时间】:2023-03-11 00:51:01
【问题描述】:

在图片中,您可以看到最后一个条形图是 11 月,但 plotly 调用的是 2021 年 11 月 10 月。为什么以及如何解决?

orders_month = orders[['createdAt', 'order_total_usd']]
orders_month_grouped = orders_month.groupby(pd.Grouper(key='createdAt', axis=0, freq='M')).sum().reset_index()
orders_month_grouped['value_labels'] = orders_month_grouped['order_total_usd'].map('${:,.0f}'.format)
fig2 = px.bar(orders_month_grouped, x='createdAt', y="order_total_usd",   text='value_labels')
fig2.update_layout(
    title="Order Value by Month",
    xaxis_title="Month",
    yaxis_title="Order Value"
    )
fig2.update_yaxes(range=[350000, 600000])
fig2.show()

更新: 找到了这个解决方法

看来我可以用这个来解决它

    fig2.layout.xaxis.tick0 = orders_month_grouped['createdAt'].iloc[0]
    fig2.layout.xaxis.dtick = 'M1'

但我为什么必须这样做(为什么错了)?

添加数据图像

【问题讨论】:

  • 您能否提供一些说明问题的输入数据?最好是orders_month_grouped,它已经对数据进行了分组。
  • @Shaido 添加图片以显示数据
  • 如果您认为您自己的解决方案足以很好地回答您的问题,请为您自己的问题创建一个答案并接受它作为正确答案,以便它在系统中被屏蔽为已回答。
  • 当然,但我仍然想知道为什么我需要手动更正这个?这是情节的一个错误吗?

标签: python plotly pandas-groupby


【解决方案1】:

这里有一个修复:添加 xaxis_tick0...

fig2 = px.bar(orders_month_grouped, x='createdAt', y="order_total_usd",   text='value_labels')
fig2.update_layout(
    title="Order Value by Month",
    xaxis_title="Month",
    yaxis_title="Order Value",
    xaxis_tick0 = orders_month_grouped['createdAt'].iloc[0],
    xaxis_dtick = 'M1'
)

【讨论】:

    猜你喜欢
    • 2013-11-23
    • 2021-07-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-06-13
    • 1970-01-01
    • 2022-11-14
    • 1970-01-01
    相关资源
    最近更新 更多