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

Skip to content

Commit 2ef6c04

Browse files
Tests that make_subplots checks spacing against number of rows and cols
The tests are failing for some reason like the regex doesn't match when it should...
1 parent 9eded54 commit 2ef6c04

File tree

2 files changed

+30
-2
lines changed

2 files changed

+30
-2
lines changed

packages/python/plotly/plotly/subplots.py

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -637,6 +637,21 @@ def _checks(item, defaults):
637637

638638
max_subplot_ids = _get_initial_max_subplot_ids()
639639

640+
# Check to see that number of rows and columns does not conflict with
641+
# requested vertical_spacing and horizontal_spacing
642+
def _check_row_col_spacing(n, spacing):
643+
if (n > 1) and (spacing > (1 / (n - 1))):
644+
return False
645+
return True
646+
647+
if not _check_row_col_spacing(rows, vertical_spacing):
648+
raise ValueError(
649+
"Vertical spacing must be less than or equal to 1 / (rows - 1)."
650+
)
651+
if not _check_row_col_spacing(cols, horizontal_spacing):
652+
raise ValueError(
653+
"Horizontal spacing must be less than or equal to 1 / (cols - 1)."
654+
)
640655
# Loop through specs -- (r, c) <-> (row, col)
641656
for r, spec_row in enumerate(specs):
642657
for c, spec in enumerate(spec_row):
@@ -1350,9 +1365,9 @@ def _set_trace_grid_reference(trace, layout, grid_ref, row, col, secondary_y=Fal
13501365
raise ValueError(
13511366
"""\
13521367
Trace type '{typ}' is not compatible with subplot type '{subplot_type}'
1353-
at grid position ({row}, {col})
1368+
at grid position ({row}, {col})
13541369
1355-
See the docstring for the specs argument to plotly.subplots.make_subplots
1370+
See the docstring for the specs argument to plotly.subplots.make_subplots
13561371
for more information on subplot types""".format(
13571372
typ=trace.type,
13581373
subplot_type=subplot_refs[0].subplot_type,

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

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,19 @@ def test_row_width_not_list_of_correct_numbers(self):
114114
with self.assertRaises(Exception):
115115
tls.make_subplots(rows=2, cols=2, row_heights=[1])
116116

117+
def test_bad_row_spacing(self):
118+
with self.assertRaisesRegex(
119+
ValueError, "Vertical spacing must be less than or equal to 1 / (rows - 1)."
120+
):
121+
tls.make_subplots(rows=3, vertical_spacing=1 / 2 + 1e-6)
122+
123+
def test_bad_col_spacing(self):
124+
with self.assertRaisesRegex(
125+
ValueError,
126+
"Horizontal spacing must be less than or equal to 1 / (cols - 1).",
127+
):
128+
tls.make_subplots(cols=3, horizontal_spacing=1 / 2 + 1e-6)
129+
117130
def test_single_plot(self):
118131
expected = Figure(
119132
data=Data(),

0 commit comments

Comments
 (0)