You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
fig.add_vline on date axis fails with date, timestamp and string as x value #4923
I build a line chart with dates on the x-axis. I want to add a vertical line at a certain x-position. Using a timestamp, date or date-formated string as value for x fails.
File "...\src\plots.py", line 45, in plot_score
fig.add_vline(
File "...\.venv\Lib\site-packages\plotly\graph_objs\_figure.py", line 1049, in add_vline
return super(Figure, self).add_vline(
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "...\.venv\Lib\site-packages\plotly\basedatatypes.py", line 4095, in add_vline
self._process_multiple_axis_spanning_shapes(
File "...\.venv\Lib\site-packages\plotly\basedatatypes.py", line 4036, in _process_multiple_axis_spanning_shapes
augmented_annotation = shapeannotation.axis_spanning_shape_annotation(
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "...\.venv\Lib\site-packages\plotly\shapeannotation.py", line 216, in axis_spanning_shape_annotation
shape_dict = annotation_params_for_line(
^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "...\.venv\Lib\site-packages\plotly\shapeannotation.py", line 63, in annotation_params_for_line
eX = _mean(X)
^^^^^^^^
File "...\.venv\Lib\site-packages\plotly\shapeannotation.py", line 7, in _mean
return float(sum(x)) / len(x)
^^^^^^
TypeError: unsupported operand type(s) for +: 'int' and 'str'
I will be stating the obvious here: in annotation_param_per_line the computation of mean X and mean Y could be postponed for only those cases that actually requires them. In this way at least all other operation could run, leaving out the following combinations:
vline with annotation_position="left" or annotation_position="right"
hline with annotation_position="top" or annotation_position="bottom"
For these cases a check on the X and/or Y type to be numeric could be performed (maybe even in _prepare_position) and a more informative error message provided. @gvwilson what do you think?
This bug was reported a few years ago; there's a workaround on the issue: #3065
Unfortunately, this bug is still very real. :(
I experience the same error with add_vrect or add_vline for datetime.datetime objects for the x axis. Tested today on python 3.10.6 using plotly 5.22.0 and python 3.9.15 with plotly 5.24.1.
Activity
gvwilson commentedon Dec 3, 2024
@stoney95 please let us know which version of plotly you are using - thanks.
stoney95 commentedon Dec 3, 2024
@gvwilson I am using
plotly==5.24.1andpython==3.11FBruzzesi commentedon Dec 3, 2024
I will be stating the obvious here: in annotation_param_per_line the computation of mean X and mean Y could be postponed for only those cases that actually requires them. In this way at least all other operation could run, leaving out the following combinations:
vlinewithannotation_position="left"orannotation_position="right"hlinewithannotation_position="top"orannotation_position="bottom"For these cases a check on the X and/or Y type to be numeric could be performed (maybe even in
_prepare_position) and a more informative error message provided. @gvwilson what do you think?gvwilson commentedon Dec 4, 2024
I will defer to @emilykl on this one - sadly (tragically) I'm not familiar enough with the code for any of this to be obvious to me any longer :-(
austin-spagnolo commentedon Dec 13, 2024
This bug was reported a few years ago; there's a workaround on the issue: #3065
Unfortunately, this bug is still very real. :(
I experience the same error with
add_vrectoradd_vlinefordatetime.datetimeobjects for the x axis. Tested today onpython 3.10.6usingplotly 5.22.0andpython 3.9.15withplotly 5.24.1.FBruzzesi commentedon Dec 19, 2024
@emilykl if no one is taking a look at this I am happy to give it a try.
I wonder what's the expected behavior for date, datetime, timestamp and string types for the cases mentioned above, namely:
vlinewithannotation_position="left"orannotation_position="right"hlinewithannotation_position="top"orannotation_position="bottom"emilykl commentedon Aug 30, 2026
Resolved by #5508 and first released in plotly.py v6.8.0.