I want to plot a continuous 'Time' column against dates on a simple timeseries linechart in plotly express. The 'Time' column starts out as a string, in the format HH:MM:SS, but when I plot this outright it is treated as discrete values. To remedy this and make it continuous I tried converting to timedelta data type, using pd.to_timedelta
. This correctly converts my column into nanoseconds and the shape of the line and axis looks correct. However I do not want to display the axis as nanoseconds, or any other fixed unit, I would like it to display as HH:MM:SS, but am unsure how I might format this.
Asked
Active
Viewed 2,226 times
4

marc_s
- 732,580
- 175
- 1,330
- 1,459

artfulinfo
- 108
- 1
- 11
-
Check this out. Maybe you could covert the timedeltas to strings (link [here](https://stackoverflow.com/questions/538666/format-timedelta-to-string)) and then Plotly Express would handle the times correctly. – Jacob K Mar 22 '21 at 01:49
-
try this:`fig.update_layout(xaxis_tickformat='%H-%M-%S')` – r-beginners Mar 22 '21 at 02:09
-
@r-beginners thanks I was hopeful for this, unfortunately it gave me this [output](https://imgur.com/QUbWk3y) – artfulinfo Mar 22 '21 at 02:19
-
I don't understand why it's the Y axis, it's specified in the xaxis_tickformat, right? – r-beginners Mar 22 '21 at 02:25
-
@JacobK I tried it originally as a string, but seems not to work as it is not a continuous scale. I could sort them I suppose so they at least appeared in the correct order, but I don't think it would scale precisely, so not really what I'm after – artfulinfo Mar 22 '21 at 02:26
-
@r-beginners the timedelta field I need formatted that way is 'Time', so the Y axis. Apologies, realise I called it 'duration' in the original post – artfulinfo Mar 22 '21 at 02:27
-
Then I think we can change to this.`fig.update_layout(yaxis_tickformat='%H-%M-%S')` – r-beginners Mar 22 '21 at 02:35
-
Once again, the format of the y-axis is a date/time format, right? – r-beginners Mar 22 '21 at 02:36
-
@r-beginners Not quite, they are a timedelta - so they are presented like this when I print "0 days 01:07:56" but I believe it is stored as nanoseconds (ns), as this is the default for [pd.to_timedelta](https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.Timedelta.html) – artfulinfo Mar 22 '21 at 02:41
-
It's more reliable to convert the original data into time format. – r-beginners Mar 22 '21 at 02:53
-
1@r-beginners oh wow, yep I changed `pd.to_timedelta` to `pd.to_datetime` and your formatting code worked. Thanks! – artfulinfo Mar 22 '21 at 02:59
1 Answers
1
There is no easy way to do this in express. But if you use Plotly go you can use this piece of code directly from their website.
fig.update_xaxes(
ticktext=["End of Q1", "End of Q2", "End of Q3", "End of Q4"],
tickvals=["2016-04-01", "2016-07-01", "2016-10-01"],
)
It will map ticket vals display name to the matching entry in ticket text. This should maintain scale if tickvals are a scalar. In addition in this example you can have these pieces of text loop every year after.
Here is the link to their website: Plotly Axes with Labels

Christopher Piccoli
- 11
- 1