Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
implement pull request #3731 and add tests to fix #3065
  • Loading branch information
amiteitan committed May 20, 2025
commit d03b087202bd6d35c7a76e7dbbd7c2a86d582380
3 changes: 3 additions & 0 deletions plotly/shapeannotation.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@
def _mean(x):
if len(x) == 0:
raise ValueError("x must have positive length")

if len(x) == 2 and x[0] == x[1]:
return x[0]
return float(sum(x)) / len(x)


Expand Down
19 changes: 19 additions & 0 deletions tests/test_optional/test_autoshapes/test_annotated_shapes.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,25 @@ def multi_plot_fixture():
fig.add_trace(go.Scatter(x=[], y=[]), row=r, col=c)
return fig

from datetime import datetime, timedelta
def test_add_shape_with_dates():
start_date = datetime(2025, 1, 1)
end_date = datetime(2025, 10, 10)
dates = []
current_date = start_date
while current_date <= end_date:
dates.append(current_date.strftime("%Y-%m-%d"))
current_date += timedelta(weeks=4)
print(dates)
fig = go.Figure(
data=[go.Scatter(x=[x for x in range(1, 20, 2)], y=dates)],
layout=go.Layout(
title=go.layout.Title(text="A Figure Specified By A Graph Object")
)
)
fig.add_vline(x="2025-06-24", annotation_text="test")
assert len(fig.layout.annotations) == 1


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