Skip to content

Commit d03b087

Browse files
committed
implement pull request plotly#3731 and add tests to fix plotly#3065
1 parent 1b7efe2 commit d03b087

File tree

2 files changed

+22
-0
lines changed

2 files changed

+22
-0
lines changed

plotly/shapeannotation.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,9 @@
44
def _mean(x):
55
if len(x) == 0:
66
raise ValueError("x must have positive length")
7+
8+
if len(x) == 2 and x[0] == x[1]:
9+
return x[0]
710
return float(sum(x)) / len(x)
811

912

tests/test_optional/test_autoshapes/test_annotated_shapes.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,25 @@ def multi_plot_fixture():
5151
fig.add_trace(go.Scatter(x=[], y=[]), row=r, col=c)
5252
return fig
5353

54+
from datetime import datetime, timedelta
55+
def test_add_shape_with_dates():
56+
start_date = datetime(2025, 1, 1)
57+
end_date = datetime(2025, 10, 10)
58+
dates = []
59+
current_date = start_date
60+
while current_date <= end_date:
61+
dates.append(current_date.strftime("%Y-%m-%d"))
62+
current_date += timedelta(weeks=4)
63+
print(dates)
64+
fig = go.Figure(
65+
data=[go.Scatter(x=[x for x in range(1, 20, 2)], y=dates)],
66+
layout=go.Layout(
67+
title=go.layout.Title(text="A Figure Specified By A Graph Object")
68+
)
69+
)
70+
fig.add_vline(x="2025-06-24", annotation_text="test")
71+
assert len(fig.layout.annotations) == 1
72+
5473

5574
# Make sure adding a shape without specifying an annotation doesn't add any annotations
5675
def test_add_shape_no_annotation(multi_plot_fixture):

0 commit comments

Comments
 (0)