简体   繁体   中英

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

标签不正确

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.

布局仅适用于第一个

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'})

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM