简体   繁体   English

如何在 Plotly 时间轴中插入 x 轴标签?

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

Using DougR's code from the answer in my other question :使用我其他问题答案中的 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()

Using x="Date" gives me an error:使用x="Date"给我一个错误:

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

Yet it works in other charts :但它适用于其他图表

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

How can I add x and y axis labels to my timeline?如何将 x 和 y 轴标签添加到我的时间轴?

Refer to the documentation of timeline: https://plotly.com/python-api-reference/generated/plotly.express.timeline.html参考timeline的文档: https://plotly.com/python-api-reference/generated/plotly.express.timeline.html

You should specify labels and pass a dictionary of corresponding names.您应该指定标签并传递相应名称的字典。

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