I included some Text and Annotations for my Timeline plots (Gantt Charts | Python | Plotly). So far so good. However I want to position them all on the right hand side outside of the bars. When I use the ‘textposition’ argument I get the following error:
TypeError: timeline() got an unexpected keyword argument 'textposition'
Sample Code:
import plotly.express as px
import pandas as pd
df = pd.DataFrame([
dict(Task="Job A", Start='2009-01-01', Finish='2009-02-28'),
dict(Task="Job B", Start='2009-03-05', Finish='2009-04-15'),
dict(Task="Job C", Start='2009-02-20', Finish='2009-05-30')
])
fig = px.timeline(df, x_start="Start", x_end="Finish", y="Task", text='Task') # textposition argument not possible
fig.show()
Has anyone a solution?
Hi @HansPeter123,
You are not supposed to pass the textposition
argument inside the timeline()
method, changes to the text and annotation are passed into the fig.update_traces()
method.
Like this:
import plotly.express as px
import pandas as pd
df = pd.DataFrame([
dict(Task="Job A", Start='2009-01-01', Finish='2009-02-28'),
dict(Task="Job B", Start='2009-03-05', Finish='2009-04-15'),
dict(Task="Job C", Start='2009-02-20', Finish='2009-05-30')
])
fig = px.timeline(df, x_start="Start", x_end="Finish", y="Task", text='Task') # textposition argument not possible
fig.update_traces(textposition='outside')
fig.show()
1 Like
Thank you so much. This works perfectly!
1 Like
Hi @atharvakatre, I have another question regarding timeline plots. Since you solved this one, I thought you may have an idea for the other question: Px.timeline: Vertical Line to indicate certain date. Thank you very much
@HansPeter123
I have posted a solution on that thread. Please refer to it, thank you
1 Like