Reputation: 1377
In Plotly I am trying to display nice x-axis
The values in df["yearweek"]
are the weeks of the year: 202101 , 202102, ...
But in the image the display in odd format
Is there a way to just display as-is, in their raw form ?
fig = make_subplots(rows=2, cols=2,
subplot_titles=("Total Job DB Bytes","Average Job DB Bytes","Total Job DB Calls","Average Job DB Calls"))
fig.add_trace(go.Scatter(x=df["yearweek"], y=df["total_db_size"]), row=1, col=1)
fig.add_trace(go.Scatter(x=df["yearweek"], y=df["size_by_jobs"]), row=1, col=2)
fig.add_trace(go.Scatter(x=df["yearweek"], y=df["total_db_calls"]),row=2, col=1)
fig.add_trace(go.Scatter(x=df["yearweek"], y=df["calls_by_jobs"]), row=2, col=2)
fig.update_xaxes(tickmode="linear", row=1, col=1)
fig.update_xaxes(tickmode="linear", row=1, col=2)
fig.update_xaxes(tickmode="linear", row=2, col=1)
fig.update_xaxes(tickmode="linear", row=2, col=2)
fig.update_layout(height=800, width=1000, xaxis = {'type' : 'category'}, showlegend=False)
fig.show()
EDIT: Here is the full 2x2 subplot layout. Including the type change that vestland suggested below. This shows the layout only applying to the first plot and the x-axis changing order on that one too.
Upvotes: 1
Views: 3156
Reputation: 1377
Thanks to vestland's response I was able to work it out. I needed both type='category', categoryorder='category ascending' on every cell like so:
fig = make_subplots(rows=2, cols=2,
subplot_titles=("Total Job DB Bytes","Average Job DB Bytes","Total Job DB Calls","Average Job DB Calls"))
fig.add_trace(go.Scatter(x=df["yearweek"], y=df["total_db_size"]), row=1, col=1)
fig.add_trace(go.Scatter(x=df["yearweek"], y=df["size_by_jobs"]), row=1, col=2)
fig.add_trace(go.Scatter(x=df["yearweek"], y=df["total_db_calls"]),row=2, col=1)
fig.add_trace(go.Scatter(x=df["yearweek"], y=df["calls_by_jobs"]), row=2, col=2)
fig.update_xaxes(tickmode="linear", type='category', categoryorder='category ascending', row=1, col=1)
fig.update_xaxes(tickmode="linear", type='category', categoryorder='category ascending', row=1, col=2)
fig.update_xaxes(tickmode="linear", type='category', categoryorder='category ascending', row=2, col=1)
fig.update_xaxes(tickmode="linear", type='category', categoryorder='category ascending', row=2, col=2)
fig.update_layout(height=800, width=1000, showlegend=False)
fig.show()
Upvotes: 0
Reputation: 61084
Is there a way to just display as-is, in their raw form
fig.update_layout(xaxis = {'type' : 'category'})
# imports
import plotly.express as px
import plotly.graph_objects as go
import pandas as pd
# data
df = pd.DataFrame({'yearweek':[202101 , 202102, 202103],
'my_value':[1,2, 4]})
# plotly
fig = go.Figure()
fig.add_trace(go.Scatter(x=df["yearweek"], y=df["my_value"]))
# plotly x-axis type / format
fig.update_layout(xaxis = {'type' : 'category'})
Upvotes: 1