I am using plotly express to make a custom timeline graph object and I want to edit the x(time) axis to display a different time format, but it will only allow for 'datetime'. Is there any way to use a custom time type in plotly using the timeline object? Is there a better framework for this, such as matplotlib or something else?
Asked
Active
Viewed 1,002 times
1 Answers
1
It is possible to customize time format with plotly, see plotly documentation. Examples shown there use px.line()
, but formatting works with px.timeline()
the same way.
Example:
presidents = pd.DataFrame([{"name": "Clinton", "start": '1993-01-20', 'end': '2001-01-20'},
{"name": "Bush", "start": '2001-01-20', 'end': '2009-01-20'},
{"name": "Obama", "start": '2009-01-20', 'end': '2017-01-20'},
{"name": "Trump", "start": '2017-01-20', 'end': '2021-01-20'}
])
fig = px.timeline(presidents,
x_start="start",
x_end="end",
y="name"
)
fig.update_xaxes(
dtick="M48", # display x-ticks every 24 months months
tickformat="Date:%Y-%m-%d" # date format
)
fig.show()
This gives:

bb1
- 7,174
- 2
- 8
- 23