Skip to content

Commit 46b480a

Browse files
Descriptive error when subplot spacing impossible
Indicates range of values that are accepted.
1 parent a3550e6 commit 46b480a

File tree

2 files changed

+87
-0
lines changed

2 files changed

+87
-0
lines changed

Diff for: packages/python/plotly/plotly/subplots.py

+16
Original file line numberDiff line numberDiff line change
@@ -530,19 +530,35 @@ def _checks(item, defaults):
530530
)
531531
)
532532

533+
def _check_hv_spacing(dimsize, spacing, name, dimname):
534+
if spacing < 0 or spacing > 1:
535+
raise ValueError("%s spacing must be between 0 and 1." % (name,))
536+
if dimsize <= 1:
537+
return
538+
max_spacing = 1 / (dimsize - 1)
539+
if spacing > max_spacing:
540+
raise ValueError(
541+
"%s spacing cannot be greater than (1 / (%s - 1)) = %f."
542+
% (name, dimname, max_spacing)
543+
)
544+
533545
# ### horizontal_spacing ###
534546
if horizontal_spacing is None:
535547
if has_secondary_y:
536548
horizontal_spacing = 0.4 / cols
537549
else:
538550
horizontal_spacing = 0.2 / cols
551+
# check horizontal_spacing can be satisfied:
552+
_check_hv_spacing(cols, horizontal_spacing, "Horizontal", "cols")
539553

540554
# ### vertical_spacing ###
541555
if vertical_spacing is None:
542556
if subplot_titles is not None:
543557
vertical_spacing = 0.5 / rows
544558
else:
545559
vertical_spacing = 0.3 / rows
560+
# check vertical_spacing can be satisfied:
561+
_check_hv_spacing(rows, vertical_spacing, "Vertical", "rows")
546562

547563
# ### subplot titles ###
548564
if subplot_titles is None:

Diff for: packages/python/plotly/plotly/tests/test_core/test_subplots/test_make_subplots.py

+71
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
from __future__ import absolute_import
22

33
from unittest import TestCase
4+
import pytest
45
from plotly.graph_objs import (
56
Annotation,
67
Annotations,
@@ -1934,3 +1935,73 @@ def test_if_passed_figure(self):
19341935
fig2sp = subplots.make_subplots(2, 2)
19351936
assert id(fig2sp) != id(figsp)
19361937
assert fig2sp.layout == figsp.layout
1938+
1939+
1940+
def test_make_subplots_spacing_error():
1941+
# check exception describing maximum value for horizontal_spacing or
1942+
# vertical_spacing is raised when spacing exceeds that value
1943+
with pytest.raises(
1944+
ValueError,
1945+
match=(
1946+
"^%s spacing cannot be greater than \(1 / \(%s - 1\)\) = %f.$"
1947+
% ("Vertical", "rows", 0.02)
1948+
).replace(".", "\."),
1949+
):
1950+
fig = subplots.make_subplots(51, 1, vertical_spacing=0.0201)
1951+
with pytest.raises(
1952+
ValueError,
1953+
match=(
1954+
"^%s spacing cannot be greater than \(1 / \(%s - 1\)\) = %f.$"
1955+
% ("Horizontal", "cols", 0.02)
1956+
).replace(".", "\."),
1957+
):
1958+
fig = subplots.make_subplots(1, 51, horizontal_spacing=0.0201)
1959+
try:
1960+
fig = subplots.make_subplots(51, 1, vertical_spacing=0.0200)
1961+
except ValueError:
1962+
# This shouldn't happen so we assert False to force failure
1963+
assert False
1964+
try:
1965+
fig = subplots.make_subplots(1, 51, horizontal_spacing=0.0200)
1966+
except ValueError:
1967+
# This shouldn't happen so we assert False to force failure
1968+
assert False
1969+
# make sure any value between 0 and 1 works for horizontal_spacing if cols is 1
1970+
try:
1971+
fig = subplots.make_subplots(1, 1, horizontal_spacing=0)
1972+
except ValueError:
1973+
# This shouldn't happen so we assert False to force failure
1974+
assert False
1975+
try:
1976+
fig = subplots.make_subplots(1, 1, horizontal_spacing=1)
1977+
except ValueError:
1978+
# This shouldn't happen so we assert False to force failure
1979+
assert False
1980+
# make sure any value between 0 and 1 works for horizontal_spacing if cols is 1
1981+
try:
1982+
fig = subplots.make_subplots(1, 1, horizontal_spacing=0)
1983+
except ValueError:
1984+
# This shouldn't happen so we assert False to force failure
1985+
assert False
1986+
# make sure any value between 0 and 1 works for horizontal_spacing if cols is 1
1987+
try:
1988+
fig = subplots.make_subplots(1, 1, horizontal_spacing=1)
1989+
except ValueError:
1990+
# This shouldn't happen so we assert False to force failure
1991+
assert False
1992+
with pytest.raises(
1993+
ValueError, match="^Horizontal spacing must be between 0 and 1\.$"
1994+
):
1995+
fig = subplots.make_subplots(1, 1, horizontal_spacing=-0.01)
1996+
with pytest.raises(
1997+
ValueError, match="^Horizontal spacing must be between 0 and 1\.$"
1998+
):
1999+
fig = subplots.make_subplots(1, 1, horizontal_spacing=1.01)
2000+
with pytest.raises(
2001+
ValueError, match="^Vertical spacing must be between 0 and 1\.$"
2002+
):
2003+
fig = subplots.make_subplots(1, 1, vertical_spacing=-0.01)
2004+
with pytest.raises(
2005+
ValueError, match="^Vertical spacing must be between 0 and 1\.$"
2006+
):
2007+
fig = subplots.make_subplots(1, 1, vertical_spacing=1.01)

0 commit comments

Comments
 (0)