Skip to content

Commit c006c1f

Browse files
Tests for go.Figure.set_subplots and figure argument to make_subplots
1 parent 9d350bc commit c006c1f

File tree

2 files changed

+28
-0
lines changed

2 files changed

+28
-0
lines changed

packages/python/plotly/plotly/tests/test_core/test_graph_objs/test_figure.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
from __future__ import absolute_import
22

33
import plotly.graph_objects as go
4+
from plotly.subplots import make_subplots
45
from plotly.tests.utils import TestCaseNoTemplate
56

67

@@ -199,3 +200,20 @@ def test_update_overwrite_data(self):
199200
fig.to_plotly_json()["data"],
200201
[{"type": "scatter", "y": [1, 3, 2], "line": {"color": "yellow"}}],
201202
)
203+
204+
def test_set_subplots(self):
205+
# Test that it works the same as make_subplots for a simple call
206+
fig0 = go.Figure()
207+
fig0_sp = make_subplots(2, 2)
208+
fig0.set_subplots(2, 2)
209+
assert fig0.layout == fig0_sp.layout
210+
# Test that it accepts the same arguments as make_subplots
211+
fig1 = go.Figure()
212+
fig1.set_subplots(rows=2, cols=2, horizontal_spacing=0.25, vertical_spacing=0.1)
213+
fig1_sp = make_subplots(
214+
rows=2, cols=2, horizontal_spacing=0.25, vertical_spacing=0.1
215+
)
216+
assert fig1.layout == fig1_sp.layout
217+
# Test that calling on a figure that already has subplots throws an error.
218+
with self.assertRaisesRegex(ValueError, "^This figure already has subplots\.$"):
219+
fig1.set_subplots(2, 3)

packages/python/plotly/plotly/tests/test_core/test_subplots/test_make_subplots.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1924,3 +1924,13 @@ def test_secondary_y_subplots(self):
19241924
expected.update_traces(uid=None)
19251925

19261926
self.assertEqual(fig.to_plotly_json(), expected.to_plotly_json())
1927+
1928+
def test_if_passed_figure(self):
1929+
# assert it returns the same figure it was passed
1930+
fig = Figure()
1931+
figsp = subplots.make_subplots(2, 2, figure=fig)
1932+
assert id(fig) == id(figsp)
1933+
# assert the layout is the same when it returns its own figure
1934+
fig2sp = subplots.make_subplots(2, 2)
1935+
assert id(fig2sp) != id(figsp)
1936+
assert fig2sp.layout == figsp.layout

0 commit comments

Comments
 (0)