|
5 | 5 |
|
6 | 6 | import plotly.graph_objs as go
|
7 | 7 | from plotly.subplots import make_subplots
|
| 8 | + |
8 | 9 | import pytest
|
9 | 10 |
|
10 | 11 |
|
@@ -351,6 +352,64 @@ def test_no_exclude_empty_subplots():
|
351 | 352 | assert fig.layout[k][3]["xref"] == "x4" and fig.layout[k][3]["yref"] == "y4"
|
352 | 353 |
|
353 | 354 |
|
| 355 | +def test_supplied_yref_on_single_plot_subplot(): |
| 356 | + ### test a (1,1) subplot figure object |
| 357 | + fig = make_subplots(1, 1) |
| 358 | + fig.add_trace(go.Scatter(x=[1, 2, 3, 4], y=[1, 2, 2, 1])) |
| 359 | + fig.add_trace(go.Scatter(x=[1, 2, 3, 4], y=[4, 3, 2, 1], yaxis="y2")) |
| 360 | + fig.update_layout( |
| 361 | + yaxis=dict(title="yaxis1 title"), |
| 362 | + yaxis2=dict(title="yaxis2 title", overlaying="y", side="right"), |
| 363 | + ) |
| 364 | + # add horizontal line on y2. Secondary_y can be True or False when yref is supplied |
| 365 | + fig.add_hline(y=3, yref="y2", secondary_y=True) |
| 366 | + assert fig.layout["shapes"][0]["yref"] == "y2" |
| 367 | + |
| 368 | + |
| 369 | +def test_supplied_yref_on_non_subplot_figure_object(): |
| 370 | + ### test a non-subplot figure object from go.Figure |
| 371 | + trace1 = go.Scatter(x=[1, 2, 3, 4], y=[1, 2, 2, 1]) |
| 372 | + trace2 = go.Scatter(x=[1, 2, 3, 4], y=[4, 3, 2, 1], yaxis="y2") |
| 373 | + data = [trace1, trace2] |
| 374 | + layout = go.Layout( |
| 375 | + yaxis=dict(title="yaxis1 title"), |
| 376 | + yaxis2=dict(title="yaxis2 title", overlaying="y", side="right"), |
| 377 | + ) |
| 378 | + fig = go.Figure(data=data, layout=layout) |
| 379 | + # add horizontal line on y2. Secondary_y can be True or False when yref is supplied |
| 380 | + fig.add_hline(y=3, yref="y2", secondary_y=False) |
| 381 | + assert fig.layout["shapes"][0]["yref"] == "y2" |
| 382 | + |
| 383 | + |
| 384 | +def test_supplied_yref_on_multi_plot_subplot(): |
| 385 | + ### test multiple subploted figure object with subplots.make_subplots |
| 386 | + fig = make_subplots( |
| 387 | + rows=1, |
| 388 | + cols=2, |
| 389 | + shared_yaxes=False, |
| 390 | + specs=[[{"secondary_y": True}, {"secondary_y": True}]], |
| 391 | + ) |
| 392 | + ### Add traces to the first subplot |
| 393 | + fig.add_trace(go.Scatter(x=[1, 2, 3], y=[1, 2, 3]), row=1, col=1) |
| 394 | + fig.add_trace( |
| 395 | + go.Scatter(x=[1, 2, 3], y=[3, 2, 1], yaxis="y2"), row=1, col=1, secondary_y=True |
| 396 | + ) |
| 397 | + ### Add traces to the second subplot |
| 398 | + fig.add_trace(go.Scatter(x=[1, 2, 3], y=[1, 2, 3], yaxis="y"), row=1, col=2) |
| 399 | + fig.add_trace( |
| 400 | + go.Scatter(x=[1, 2, 3], y=[1, 1, 2], yaxis="y2"), row=1, col=2, secondary_y=True |
| 401 | + ) |
| 402 | + # add a horizontal line on both subplots on their respective secondary y. |
| 403 | + # When using the subplots.make_subplots() method yref parameter should NOT be supplied per docstring instructions. |
| 404 | + # Instead secondary_y specs and secondary_y parameter MUST be True to plot on secondary y |
| 405 | + fig.add_hline(y=2, row=1, col=1, secondary_y=True) |
| 406 | + fig.add_hline(y=1, row=1, col=2, secondary_y=True) |
| 407 | + assert fig.layout["shapes"][0]["yref"] == "y2" |
| 408 | + assert fig.layout["shapes"][0]["xref"] == "x domain" |
| 409 | + assert fig.layout["shapes"][1]["yref"] == "y4" |
| 410 | + assert fig.layout["shapes"][1]["xref"] == "x2 domain" |
| 411 | + |
| 412 | + |
354 | 413 | @pytest.fixture
|
355 | 414 | def select_annotations_integer():
|
356 | 415 | fig = make_subplots(2, 3)
|
|
0 commit comments