簡體   English   中英

如何在 Plotly 時間軸中插入 x 軸標簽?

[英]How to insert x axis labels in a Plotly timeline?

使用我其他問題答案中的 DougR 代碼

import plotly.express as px
import pandas as pd

df = pd.DataFrame([
    dict(Task="Job A", Type="Plan", Start='2009-01-01', Finish='2009-02-28'),
    dict(Task="Job A", Type="Actual", Start='2009-01-05', Finish='2009-02-15'),
    dict(Task="Job C", Type="Plan", Start='2009-02-20', Finish='2009-05-30'),
    dict(Task="Job C", Type="Actual", Start='2009-02-23', Finish='2009-06-04')
])

colours = {}
colours['Plan'] =  'red' # or 'rgb(220, 0, 0)' for rgb colours
colours['Actual'] = 'blue'

fig = px.timeline(df,
                  x="Date",
                  x_start="Start", 
                  x_end="Finish", 
                  y="Task", 
                  color='Type',
                  color_discrete_map = colours
                )

fig.data[1].width=0.5 # update the width of the second trace

fig.update_yaxes(autorange="reversed") # otherwise tasks are listed from the bottom up

fig.show()

使用x="Date"給我一個錯誤:

TypeError: timeline() got an unexpected keyword argument 'x'

但它適用於其他圖表

fig = px.line(df, x='date', y="GOOG")

如何將 x 和 y 軸標簽添加到我的時間軸?

參考timeline的文檔: https://plotly.com/python-api-reference/generated/plotly.express.timeline.html

您應該指定標簽並傳遞相應名稱的字典。

fig = px.timeline(df,
                  x_start="Start", 
                  x_end="Finish", 
                  y="Task", 
                  color='Type',
                  color_discrete_map = colours
                )
fig.update_layout(xaxis_title="Date") #to add a title to xaxis

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM