简体   繁体   English

Plotly 中的完整字符串 x 轴标签

[英]Full String x-axis labels in Plotly

In Plotly I am trying to display nice x-axis在 Plotly 我试图显示漂亮的 x 轴

The values in df["yearweek"] are the weeks of the year: 202101, 202102, ... df["yearweek"]中的值是一年中的第几周: 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.编辑:这是完整的 2x2 子图布局。 Including the type change that vestland suggested below.包括下面vestland 建议的类型更改。 This shows the layout only applying to the first plot and the x-axis changing order on that one too.这显示了仅适用于第一个 plot 的布局以及该布局上的 x 轴更改顺序。

布局仅适用于第一个

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:我在每个单元格上都需要 type='category', categoryorder='category ascending' ,如下所示:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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