Skip to content

fig.add_vline on date axis fails with date, timestamp and string as x value #4923

Description

@stoney95

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.

Example

import plotly.graph_objects as go

fig = go.Figure()

x = ["2024-08-01", "2024-09-01, "2024-0-01"]
y = [1,3,2]

fig.add_trace(go.Scatter(x=x, y=y))

fig.add_vline(x="2024-05-01", annotation_text="Example")

Error Message

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'

Expected behavior

The line is added at the provided date.

Additional context

Using fig.add_vrect with dates works

Activity

  1. gvwilson commented on Dec 3, 2024

    @gvwilson
    Contributor

    @stoney95 please let us know which version of plotly you are using - thanks.

  2. stoney95 commented on Dec 3, 2024

    @stoney95
    Author

    @gvwilson I am using plotly==5.24.1 and python==3.11

  3. FBruzzesi commented on Dec 3, 2024

    @FBruzzesi
    Contributor

    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?

  4. gvwilson commented on Dec 4, 2024

    @gvwilson
    Contributor

    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 :-(

  5. austin-spagnolo commented on Dec 13, 2024

    @austin-spagnolo

    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.

  6. FBruzzesi commented on Dec 19, 2024

    @FBruzzesi
    Contributor

    @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:

    • vline with annotation_position="left" or annotation_position="right"
    • hline with annotation_position="top" or annotation_position="bottom"
  7. emilykl commented on Aug 30, 2026

    @emilykl
    Contributor

    Resolved by #5508 and first released in plotly.py v6.8.0.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    P3backlogbugsomething broken

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions