Skip to content

[Bug] add_hline and add_vline do not work with datetime and annotation_text #3494

Closed
@hidr0

Description

@hidr0

I am trying to show an hline or vline on a time series.

import datetime
import plotly.graph_objects as go
figure = go.Figure()

figure.update_layout(
    yaxis=dict(
        type="date",
        tickformat='%H:%M',
    )
)

figure.add_hline(
    annotation_text="Recommended Daily Intake",
    y=datetime.datetime.now(),
)

The error I get:

  File "..../.venv/lib/python3.9/site-packages/plotly/shapeannotation.py", line 7, in _mean
    return float(sum(x)) / len(x)
TypeError: unsupported operand type(s) for +: 'int' and 'datetime.datetime'

x is:

>>> print(x)
[datetime.datetime(2021, 12, 2, 14, 3, 29, 780450), datetime.datetime(2021, 12, 2, 14, 3, 29, 780450)]

Which means that sum does not work with datetime.datetime:

>>> sum([datetime.datetime(2021, 12, 2, 14, 3, 29, 780450), datetime.datetime(2021, 12, 2, 14, 3, 29, 780450)])
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: unsupported operand type(s) for +: 'int' and 'datetime.datetime'

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions