簡體   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