8000 Check for float values for min/max values to ax{v,h}line · matplotlib/matplotlib@140e7d9 · GitHub
[go: up one dir, main page]

Skip to content

Commit 140e7d9

Browse files
committed
Check for float values for min/max values to ax{v,h}line
1 parent ff54be0 commit 140e7d9

File tree

2 files changed

+34
-7
lines changed

2 files changed

+34
-7
lines changed

lib/matplotlib/axes/_axes.py

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -830,10 +830,10 @@ def axhline(self, y=0, xmin=0, xmax=1, **kwargs):
830830
831831
>>> axhline(y=.5, xmin=0.25, xmax=0.75)
832832
"""
833+
self._check_scalars([xmin, xmax], ['xmin', 'xmax'])
833834
if "transform" in kwargs:
834-
raise ValueError(
835-
"'transform' is not allowed as a kwarg;"
836-
+ "axhline generates its own transform.")
835+
raise ValueError("'transform' is not allowed as a kwarg; "
836+
"axhline generates its own transform.")
837837
ymin, ymax = self.get_ybound()
838838

839839
# We need to strip away the units for comparison with
@@ -899,11 +899,10 @@ def axvline(self, x=0, ymin=0, ymax=1, **kwargs):
899899
900900
>>> axvline(x=.5, ymin=0.25, ymax=0.75)
901901
"""
902-
902+
self._check_scalars([ymin, ymax], ['ymin', 'ymax'])
903903
if "transform" in kwargs:
904-
raise ValueError(
905-
"'transform' is not allowed as a kwarg;"
906-
+ "axvline generates its own transform.")
904+
raise ValueError("'transform' is not allowed as a kwarg; "
905+
"axvline generates its own transform.")
907906
xmin, xmax = self.get_xbound()
908907

909908
# We need to strip away the units for comparison with
@@ -918,6 +917,16 @@ def axvline(self, x=0, ymin=0, ymax=1, **kwargs):
918917
self._request_autoscale_view(scalex=scalex, scaley=False)
919918
return l
920919

920+
@staticmethod
921+
def _check_scalars(vals, names):
922+
# Helper method to check that vals are all scalars
923+
for val, name in zip(vals, names):
924+
try:
925+
float(val)
926+
except Exception as e:
927+
raise ValueError(f"{name} must be a single scalar value, "
928+
f"but got {val}") from e
929+
921930
@docstring.dedent_interpd
922931
def axline(self, xy1, xy2=None, *, slope=None, **kwargs):
923932
"""
@@ -1038,6 +1047,7 @@ def axhspan(self, ymin, ymax, xmin=0, xmax=1, **kwargs):
10381047
--------
10391048
axvspan : Add a vertical span across the axes.
10401049
"""
1050+
self._check_scalars([xmin, xmax], ['xmin', 'xmax'])
10411051
trans = self.get_yaxis_transform(which='grid')
10421052

10431053
# process the unit information
@@ -1098,6 +1108,7 @@ def axvspan(self, xmin, xmax, ymin=0, ymax=1, **kwargs):
10981108
>>> axvspan(1.25, 1.55, facecolor='g', alpha=0.5)
10991109
11001110
"""
1111+
self._check_scalars([ymin, ymax], ['ymin', 'ymax'])
11011112
trans = self.get_xaxis_transform(which='grid')
11021113

11031114
# process the unit information

lib/matplotlib/tests/test_axes.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4251,6 +4251,22 @@ def test_vline_limit():
42514251
assert_allclose(ax.get_ylim(), (-.1, .2))
42524252

42534253

4254+
@pytest.mark.parametrize('fv, fh, args', [[plt.axvline, plt.axhline, (1,)],
4255+
[plt.axvspan, plt.axhspan, (1, 1)]])
4256+
def test_axline_minmax(fv, fh, args):
4257+
bad_lim = matplotlib.dates.num2date(1)
4258+
# Check vertical functions
4259+
with pytest.raises(ValueError, match='ymin must be a single scalar value'):
4260+
fv(*args, ymin=bad_lim, ymax=1)
4261+
with pytest.raises(ValueError, match='ymax must be a single scalar value'):
4262+
fv(*args, ymin=1, ymax=bad_lim)
4263+
# Check horizontal functions
4264+
with pytest.raises(ValueError, match='xmin must be a single scalar value'):
4265+
fh(*args, xmin=bad_lim, xmax=1)
4266+
with pytest.raises(ValueError, match='xmax must be a single scalar value'):
4267+
fh(*args, xmin=1, xmax=bad_lim)
4268+
4269+
42544270
def test_empty_shared_subplots():
42554271
# empty plots with shared axes inherit limits from populated plots
42564272
fig, axs = plt.subplots(nrows=1, ncols=2, sharex=True, sharey=True)

0 commit comments

Comments
 (0)
0