繁体   English   中英

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

[英]Full String x-axis labels in Plotly

在 Plotly 我试图显示漂亮的 x 轴

df["yearweek"]中的值是一年中的第几周: 202101, 202102, ...

但在图像中以奇怪的格式显示

有没有办法以原始形式按原样显示?

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

标签不正确

编辑:这是完整的 2x2 子图布局。 包括下面vestland 建议的类型更改。 这显示了仅适用于第一个 plot 的布局以及该布局上的 x 轴更改顺序。

布局仅适用于第一个

有没有办法以原始形式按原样显示

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

多亏了韦斯特兰的回应,我才得以解决。 我在每个单元格上都需要 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