Skip to content

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

Closed

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'

Activity

  1. pratikmondkar commented on Jan 18, 2022

    @pratikmondkar

    Facing the same issue. This feature is especially useful for facet grids since it avoids having to add annotations for each facet individually

  2. Uberi commented on Mar 6, 2022

    @Uberi

    Looks like this is related to #3065, which also has a workaround!

  3. gvwilson commented on Jul 3, 2024

    @gvwilson
    Contributor

    Hi - we are trying to tidy up the stale issues and PRs in Plotly's public repositories so that we can focus on things that are still important to our community. Since this one has been sitting for several years, I'm going to close it; if it is still a concern, please add a comment letting us know what recent version of our software you've checked it with so that I can reopen it and add it to our backlog. Thanks for your help - @gvwilson

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

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions