Using DougR's code from the answer in my other question:
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:
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?
Refer to the documentation of 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