Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
stoney95 opened this issue Dec 3, 2024 · 6 comments
Labels
bug something broken P3 backlog

Comments

@stoney95
Copy link

stoney95 commented Dec 3, 2024

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

@gvwilson gvwilson added P3 backlog bug something broken labels Dec 3, 2024
@gvwilson
Copy link
Contributor

gvwilson commented Dec 3, 2024

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

@stoney95
Copy link
Author

stoney95 commented Dec 3, 2024

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

@FBruzzesi
Copy link
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?

@gvwilson
Copy link
Contributor

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

@austinspagnolo
Copy link

austinspagnolo commented 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_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.

@FBruzzesi
Copy link
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"

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug something broken P3 backlog
Projects
None yet
Development

No branches or pull requests

4 participants