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

Skip to content

Commit 7b574c3

Browse files
dstansbyQuLogic
andauthored
Check for float values for min/max values to ax{v,h}line (#17822)
* Check for float values for min/max values to ax{v,h}line * kwarg > keyword argument Co-authored-by: Elliott Sales de Andrade <[email protected]> Co-authored-by: Elliott Sales de Andrade <[email protected]>
1 parent 97c26b0 commit 7b574c3

File tree

2 files changed

+33
-7
lines changed

2 files changed

+33
-7
lines changed

lib/matplotlib/axes/_axes.py

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
import matplotlib.ticker as mticker
3030
import matplotlib.transforms as mtransforms
3131
import matplotlib.tri as mtri
32+
import matplotlib.units as munits
3233
from matplotlib import _preprocess_data, rcParams
3334
from matplotlib.axes._base import _AxesBase, _process_plot_format
3435
from matplotlib.axes._secondary_axes import SecondaryAxis
@@ -827,10 +828,10 @@ def axhline(self, y=0, xmin=0, xmax=1, **kwargs):
827828
828829
>>> axhline(y=.5, xmin=0.25, xmax=0.75)
829830
"""
831+
self._check_no_units([xmin, xmax], ['xmin', 'xmax'])
830832
if "transform" in kwargs:
831-
raise ValueError(
832-
"'transform' is not allowed as a kwarg;"
833-
+ "axhline generates its own transform.")
833+
raise ValueError("'transform' is not allowed as a keyword argument; "
834+
"axhline generates its own transform.")
834835
ymin, ymax = self.get_ybound()
835836

836837
# We need to strip away the units for comparison with
@@ -896,11 +897,10 @@ def axvline(self, x=0, ymin=0, ymax=1, **kwargs):
896897
897898
>>> axvline(x=.5, ymin=0.25, ymax=0.75)
898899
"""
899-
900+
self._check_no_units([ymin, ymax], ['ymin', 'ymax'])
900901
if "transform" in kwargs:
901-
raise ValueError(
902-
"'transform' is not allowed as a kwarg;"
903-
+ "axvline generates its own transform.")
902+
raise ValueError("'transform' is not allowed as a keyword argument; "
903+
"axvline generates its own transform.")
904904
xmin, xmax = self.get_xbound()
905905

906906
# We need to strip away the units for comparison with
@@ -915,6 +915,14 @@ def axvline(self, x=0, ymin=0, ymax=1, **kwargs):
915915
self._request_autoscale_view(scalex=scalex, scaley=False)
916916
return l
917917

918+
@staticmethod
919+
def _check_no_units(vals, names):
920+
# Helper method to check that vals are not unitized
921+
for val, name in zip(vals, names):
922+
if not munits._is_natively_supported(val):
923+
raise ValueError(f"{name} must be a single scalar value, "
924+
f"but got {val}")
925+
918926
@docstring.dedent_interpd
919927
def axline(self, xy1, xy2=None, *, slope=None, **kwargs):
920928
"""
@@ -1035,6 +1043,7 @@ def axhspan(self, ymin, ymax, xmin=0, xmax=1, **kwargs):
10351043
--------
10361044
axvspan : Add a vertical span across the axes.
10371045
"""
1046+
self._check_no_units([xmin, xmax], ['xmin', 'xmax'])
10381047
trans = self.get_yaxis_transform(which='grid')
10391048

10401049
# process the unit information
@@ -1095,6 +1104,7 @@ def axvspan(self, xmin, xmax, ymin=0, ymax=1, **kwargs):
10951104
>>> axvspan(1.25, 1.55, facecolor='g', alpha=0.5)
10961105
10971106
"""
1107+
self._check_no_units([ymin, ymax], ['ymin', 'ymax'])
10981108
trans = self.get_xaxis_transform(which='grid')
10991109

11001110
# process the unit information

lib/matplotlib/tests/test_axes.py

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

43414341

4342+
@pytest.mark.parametrize('fv, fh, args', [[plt.axvline, plt.axhline, (1,)],
4343+
[plt.axvspan, plt.axhspan, (1, 1)]])
4344+
def test_axline_minmax(fv, fh, args):
4345+
bad_lim = matplotlib.dates.num2date(1)
4346+
# Check vertical functions
4347+
with pytest.raises(ValueError, match='ymin must be a single scalar value'):
4348+
fv(*args, ymin=bad_lim, ymax=1)
4349+
with pytest.raises(ValueError, match='ymax must be a single scalar value'):
4350+
fv(*args, ymin=1, ymax=bad_lim)
4351+
# Check horizontal functions
4352+
with pytest.raises(ValueError, match='xmin must be a single scalar value'):
4353+
fh(*args, xmin=bad_lim, xmax=1)
4354+
with pytest.raises(ValueError, match='xmax must be a single scalar value'):
4355+
fh(*args, xmin=1, xmax=bad_lim)
4356+
4357+
43424358
def test_empty_shared_subplots():
43434359
# empty plots with shared axes inherit limits from populated plots
43444360
fig, axs = plt.subplots(nrows=1, ncols=2, sharex=True, sharey=True)

0 commit comments

Comments
 (0)