ChatGPT
ChatGPT

Reputation: 5617

plotly is labeling my xaxis wrong. how to fix this?

in the image, you can see the final bar is Nov, but plotly is calling Oct Nov 2021. Why and how to fix?

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()

UPDATE: found this workaround

seems I can fix it with this

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

but why should I have to (why is it wrong)?

adding image of data enter image description here

enter image description here

Upvotes: 1

Views: 758

Answers (1)

ChatGPT
ChatGPT

Reputation: 5617

here's a fix: added 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'
)

Upvotes: 1

Related Questions