plotly.py: Error on using add_vline with text annotation for data with date-time x axis
Hi, I am trying to use the library for a simple visualisation for the first time, and I stumbled upon supposedly a bug trying to draw a vertical line with text annotation on a graph with a date-time x axis.
import plotly.express as px
df = px.data.stocks(indexed=True)
fig = px.line(df)
fig.add_vline(x="2018-09-24", annotation_text="test" )
fig.show()
I get the following error message:
TypeError: unsupported operand type(s) for +: 'int' and 'str'
Could anyone please help me confirm that it is indeed a bug, my version is ‘4.14.1’ ? Thank you.
About this issue
- Original URL
- State: open
- Created 3 years ago
- Reactions: 39
- Comments: 21 (4 by maintainers)
Hi @MR0205,
If you’re looking for a temporary workaround, you can get the annotation to work by converting your
x
value into milliseconds since epoch as follows:The above suggestion from @awrobel1 was great for me. In my case I simplified a tad with
x=datetime.datetime(2021,5,27).timestamp() * 1000
. No need to convert from a string first in this case.Another solution is to add a box with a specific width:
Yup this is still a very real and weird problem because if you try to do the annotation within the add_vline arguments you’ll get the exception:
but if you do it with an add_annotation function it works fine
Got the same problem today as the one before using
add_vline
with theannotation_text
parameter and I used this workaround.Hi, how can I do similar things when
x
are strings (labels) instead of dates or numbers?Hi @ClaudiaBehnke86,
Not sure, but you might be having trouble with time zones? When you run time timestamp calculation I believe that its looking for the timestamp based on your current timezone. So locally, everything works intuitively, but when you host your code in the cloud you start running on a UTC server and the times all seem to jump.
If this is the problem you just need to make sure to adjust your line based on the difference between your current time and UTC, by adding/subtracting milliseconds or using timezone aware times.
Also, make sure your code can handle time shifts for daylight savings time if relevant!