Fix add_vline text annotation with dates
Background
I would like to use add_vline with a text annotation.
Closes: https://github.com/plotly/plotly.py/issues/3065
The workaround presented in https://github.com/plotly/plotly.py/issues/3065#issuecomment-778652215 breaks if people view the plot in a difference timezone than where it was generated.
As mentioned in that ticket, here is the error when data is used for text annotations TypeError: unsupported operand type(s) for +: 'int' and 'str'
The backtrace looks like:
> /lib/python3.9/site-packages/plotly/basedatatypes.py(4085)add_vline()
-> self._process_multiple_axis_spanning_shapes(
/lib/python3.9/site-packages/plotly/basedatatypes.py(4031)_process_multiple_axis_spanning_shapes()
-> augmented_annotation = shapeannotation.axis_spanning_shape_annotation(
/lib/python3.9/site-packages/plotly/shapeannotation.py(216)axis_spanning_shape_annotation()
-> shape_dict = annotation_params_for_line(
/lib/python3.9/site-packages/plotly/shapeannotation.py(63)annotation_params_for_line()
-> eX = _mean(X)
/lib/python3.9/site-packages/plotly/shapeannotation.py(7)_mean()
-> return float(sum(x)) / len(x)
For line annotations, the same x value is passed for both x1/x2, so the mean is trivial to compute.
4084 ):
4085 -> self._process_multiple_axis_spanning_shapes(
4086 dict(type="line", x0=x, x1=x, y0=0, y1=1),
4087 row,
4088 col,
4089 "vline",
4090 exclude_empty_subplots=exclude_empty_subplots,
-> return float(sum(x)) / len(x)
(Pdb) p len(x)
2
(Pdb) p x[0] == x[1]
True
Code PR
- [x] I have read through the contributing notes and understand the structure of the package. In particular, if my PR modifies code of
plotly.graph_objects, my modifications concern thecodegenfiles and not generated files. - [ ] I have added tests (if submitting a new feature or correcting a bug) or modified existing tests.
- [ ] For a new feature, I have added documentation examples in an existing or new tutorial notebook (please see the doc checklist as well).
- [ ] I have added a CHANGELOG entry if fixing/changing/adding anything substantial.
- [ ] For a new feature or a change in behaviour, I have updated the relevant docstrings in the code to describe the feature or behaviour (please see the doc checklist as well).
Test Plan
I'm now able to use add_vline and text annotations with dates. The repro in https://github.com/plotly/plotly.py/issues/3065 works now
cc: @nicolaskruchten this is a small bugfix that is useful for us, and it looks like people have been reporting it a few times (see PR description for issue)
@marthacryan can you please have a look at this one (and if possible add a test when merging) thanks - @gvwilson
As this comment (https://github.com/plotly/plotly.py/issues/3065#issuecomment-891247458) mentions, I would suggest checking for types would be a better way than making workaround fixes to those methods.