Skip to content

Commit d95d8ee

Browse files
committed
added tests
1 parent 1d1db35 commit d95d8ee

File tree

1 file changed

+56
-0
lines changed

1 file changed

+56
-0
lines changed

packages/python/plotly/plotly/tests/test_core/test_update_objects/test_update_annotations.py

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55

66
import plotly.graph_objs as go
77
from plotly.subplots import make_subplots
8+
import plotly.express as px
89
import pytest
910

1011

@@ -351,6 +352,61 @@ def test_no_exclude_empty_subplots():
351352
assert fig.layout[k][3]["xref"] == "x4" and fig.layout[k][3]["yref"] == "y4"
352353

353354

355+
def test_supplied_yref_on_single_plot_subplot():
356+
### test a (1,1) subplot figure object from px.scatter
357+
fig = px.scatter(x=[1, 2, 3, 4], y=[1, 2, 2, 1])
358+
fig.add_trace(go.Scatter(x=[1, 2, 3, 4], y=[4, 3, 2, 1], yaxis="y2"))
359+
fig.update_layout(
360+
yaxis=dict(title="yaxis1 title"),
361+
yaxis2=dict(title="yaxis2 title", overlaying="y", side="right"),
362+
)
363+
# add horizontal line on y2. Secondary_y can be True or False
364+
fig.add_hline(y=3, yref="y2", secondary_y=True)
365+
assert fig.layout["shapes"][0]["yref"] == "y2"
366+
367+
368+
def test_supplied_yref_on_non_subplot_figure_object():
369+
### test a non-subplot figure object from go.Figure
370+
trace1 = go.Scatter(x=[1, 2, 3, 4], y=[1, 2, 2, 1])
371+
trace2 = go.Scatter(x=[1, 2, 3, 4], y=[4, 3, 2, 1], yaxis="y2")
372+
data = [trace1, trace2]
373+
layout = go.Layout(
374+
yaxis=dict(title="yaxis1 title"),
375+
yaxis2=dict(title="yaxis2 title", overlaying="y", side="right"),
376+
)
377+
fig = go.Figure(data=data, layout=layout)
378+
# add horizontal line on y2. Secondary_y can be True or False
379+
fig.add_hline(y=3, yref="y2", secondary_y=False)
380+
assert fig.layout["shapes"][0]["yref"] == "y2"
381+
382+
383+
def test_supplied_yref_on_multi_plot_subplot():
384+
### test multiple subploted figure object with subplots.make_subplots
385+
fig = make_subplots(
386+
rows=1,
387+
cols=2,
388+
shared_yaxes=False,
389+
specs=[[{"secondary_y": True}, {"secondary_y": True}]],
390+
)
391+
fig.add_trace(go.Scatter(x=[1, 2, 3], y=[1, 2, 3]), row=1, col=1)
392+
fig.add_trace(
393+
go.Scatter(x=[1, 2, 3], y=[3, 2, 1], yaxis="y2"), row=1, col=1, secondary_y=True
394+
)
395+
fig.add_trace(go.Scatter(x=[1, 2, 3], y=[1, 2, 3], yaxis="y"), row=1, col=2)
396+
fig.add_trace(
397+
go.Scatter(x=[1, 2, 3], y=[1, 1, 2], yaxis="y2"), row=1, col=2, secondary_y=True
398+
)
399+
# add a horizontal line on both subplots secondary y.
400+
# When using the subplots.make_subplots() method yref parameter should not be supplied to add_hline()
401+
# Instead Secondary_y MUST be True to plot on secondary y
402+
fig.add_hline(y=2, row=1, col=1, secondary_y=True)
403+
fig.add_hline(y=1, row=1, col=2, secondary_y=True)
404+
assert fig.layout["shapes"][0]["yref"] == "y2"
405+
assert fig.layout["shapes"][0]["xref"] == "x domain"
406+
assert fig.layout["shapes"][1]["yref"] == "y4"
407+
assert fig.layout["shapes"][1]["xref"] == "x2 domain"
408+
409+
354410
@pytest.fixture
355411
def select_annotations_integer():
356412
fig = make_subplots(2, 3)

0 commit comments

Comments
 (0)