Skip to content

Check for float values for min/max values to ax{v,h}line #17822

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jul 27, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 17 additions & 7 deletions lib/matplotlib/axes/_axes.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
import matplotlib.ticker as mticker
import matplotlib.transforms as mtransforms
import matplotlib.tri as mtri
import matplotlib.units as munits
from matplotlib import _preprocess_data, rcParams
from matplotlib.axes._base import _AxesBase, _process_plot_format
from matplotlib.axes._secondary_axes import SecondaryAxis
Expand Down Expand Up @@ -830,10 +831,10 @@ def axhline(self, y=0, xmin=0, xmax=1, **kwargs):

>>> axhline(y=.5, xmin=0.25, xmax=0.75)
"""
self._check_no_units([xmin, xmax], ['xmin', 'xmax'])
if "transform" in kwargs:
raise ValueError(
"'transform' is not allowed as a kwarg;"
+ "axhline generates its own transform.")
raise ValueError("'transform' is not allowed as a keyword argument; "
"axhline generates its own transform.")
ymin, ymax = self.get_ybound()

# We need to strip away the units for comparison with
Expand Down Expand Up @@ -899,11 +900,10 @@ def axvline(self, x=0, ymin=0, ymax=1, **kwargs):

>>> axvline(x=.5, ymin=0.25, ymax=0.75)
"""

self._check_no_units([ymin, ymax], ['ymin', 'ymax'])
if "transform" in kwargs:
raise ValueError(
"'transform' is not allowed as a kwarg;"
+ "axvline generates its own transform.")
raise ValueError("'transform' is not allowed as a keyword argument; "
"axvline generates its own transform.")
xmin, xmax = self.get_xbound()

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

@staticmethod
def _check_no_units(vals, names):
# Helper method to check that vals are not unitized
for val, name in zip(vals, names):
if not munits._is_natively_supported(val):
raise ValueError(f"{name} must be a single scalar value, "
f"but got {val}")

@docstring.dedent_interpd
def axline(self, xy1, xy2=None, *, slope=None, **kwargs):
"""
Expand Down Expand Up @@ -1038,6 +1046,7 @@ def axhspan(self, ymin, ymax, xmin=0, xmax=1, **kwargs):
--------
axvspan : Add a vertical span across the axes.
"""
self._check_no_units([xmin, xmax], ['xmin', 'xmax'])
trans = self.get_yaxis_transform(which='grid')

# process the unit information
Expand Down Expand Up @@ -1098,6 +1107,7 @@ def axvspan(self, xmin, xmax, ymin=0, ymax=1, **kwargs):
>>> axvspan(1.25, 1.55, facecolor='g', alpha=0.5)

"""
self._check_no_units([ymin, ymax], ['ymin', 'ymax'])
trans = self.get_xaxis_transform(which='grid')

# process the unit information
Expand Down
16 changes: 16 additions & 0 deletions lib/matplotlib/tests/test_axes.py
Original file line number Diff line number Diff line change
Expand Up @@ -4251,6 +4251,22 @@ def test_vline_limit():
assert_allclose(ax.get_ylim(), (-.1, .2))


@pytest.mark.parametrize('fv, fh, args', [[plt.axvline, plt.axhline, (1,)],
[plt.axvspan, plt.axhspan, (1, 1)]])
def test_axline_minmax(fv, fh, args):
bad_lim = matplotlib.dates.num2date(1)
# Check vertical functions
with pytest.raises(ValueError, match='ymin must be a single scalar value'):
fv(*args, ymin=bad_lim, ymax=1)
with pytest.raises(ValueError, match='ymax must be a single scalar value'):
fv(*args, ymin=1, ymax=bad_lim)
# Check horizontal functions
with pytest.raises(ValueError, match='xmin must be a single scalar value'):
fh(*args, xmin=bad_lim, xmax=1)
with pytest.raises(ValueError, match='xmax must be a single scalar value'):
fh(*args, xmin=1, xmax=bad_lim)


def test_empty_shared_subplots():
# empty plots with shared axes inherit limits from populated plots
fig, axs = plt.subplots(nrows=1, ncols=2, sharex=True, sharey=True)
Expand Down