Ro.
Ro.

Reputation: 1377

Full String x-axis labels in Plotly

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

Incorrect labels

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.

Layout only applies to first

Upvotes: 1

Views: 3156

Answers (2)

Ro.
Ro.

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

vestland
vestland

Reputation: 61084

Is there a way to just display as-is, in their raw form

fig.update_layout(xaxis = {'type' : 'category'})

enter image description here

# 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

Related Questions