Thanks to visit codestin.com
Credit goes to github.com

Skip to content

implement pull request #3731 and add tests to fix #3065 #5195

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

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
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
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
22 changes: 22 additions & 0 deletions tests/test_optional/test_autoshapes/test_annotated_shapes.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,28 @@ def multi_plot_fixture():
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):
multi_plot_fixture.add_hline(y=2, row="all", col="all")
Expand Down