diff --git a/doc/api/next_api_changes/deprecations/17926-ES.rst b/doc/api/next_api_changes/deprecations/17926-ES.rst new file mode 100644 index 000000000000..598737c7457a --- /dev/null +++ b/doc/api/next_api_changes/deprecations/17926-ES.rst @@ -0,0 +1,5 @@ +Invalid hatch pattern characters are no longer ignored +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + +When specifying hatching patterns, characters that are not recognized will +raise a DeprecationWarning. In the future, this will become a hard error. diff --git a/lib/matplotlib/collections.py b/lib/matplotlib/collections.py index 2dfa3efe6530..83e98ae0c870 100644 --- a/lib/matplotlib/collections.py +++ b/lib/matplotlib/collections.py @@ -15,7 +15,7 @@ import matplotlib as mpl from . import (_path, artist, cbook, cm, colors as mcolors, docstring, - lines as mlines, path as mpath, transforms) + hatch as mhatch, lines as mlines, path as mpath, transforms) import warnings @@ -141,8 +141,8 @@ def __init__(self, hatch : str, optional Hatching pattern to use in filled paths, if any. Valid strings are ['/', '\\', '|', '-', '+', 'x', 'o', 'O', '.', '*']. See - :doc:`/gallery/shapes_and_collections/hatch_demo` for the meaning - of each hatch type. + :doc:`/gallery/shapes_and_collections/hatch_style_reference` for + the meaning of each hatch type. pickradius : float, default: 5.0 If ``pickradius <= 0``, then `.Collection.contains` will return ``True`` whenever the test point is inside of one of the polygons @@ -522,6 +522,8 @@ def set_hatch(self, hatch): ---------- hatch : {'/', '\\', '|', '-', '+', 'x', 'o', 'O', '.', '*'} """ + # Use validate_hatch(list) after deprecation. + mhatch._validate_hatch_pattern(hatch) self._hatch = hatch self.stale = True diff --git a/lib/matplotlib/hatch.py b/lib/matplotlib/hatch.py index 291f35b3ff86..0d788e159f1b 100644 --- a/lib/matplotlib/hatch.py +++ b/lib/matplotlib/hatch.py @@ -1,6 +1,8 @@ """Contains classes for generating hatch patterns.""" import numpy as np + +from matplotlib import cbook from matplotlib.path import Path @@ -181,6 +183,22 @@ def __init__(self, hatch, density): ] +def _validate_hatch_pattern(hatch): + valid_hatch_patterns = set(r'-+|/\xXoO.*') + if hatch is not None: + invalids = set(hatch).difference(valid_hatch_patterns) + if invalids: + valid = ''.join(sorted(valid_hatch_patterns)) + invalids = ''.join(sorted(invalids)) + cbook.warn_deprecated( + '3.4', + message=f'hatch must consist of a string of "{valid}" or ' + 'None, but found the following invalid values ' + f'"{invalids}". Passing invalid values is deprecated ' + 'since %(since)s and will become an error %(removal)s.' + ) + + def get_path(hatchpattern, density=6): """ Given a hatch specifier, *hatchpattern*, generates Path to render diff --git a/lib/matplotlib/patches.py b/lib/matplotlib/patches.py index fe6077ce6f4a..f06f988b4790 100644 --- a/lib/matplotlib/patches.py +++ b/lib/matplotlib/patches.py @@ -8,7 +8,8 @@ import numpy as np import matplotlib as mpl -from . import artist, cbook, colors, docstring, lines as mlines, transforms +from . import (artist, cbook, colors, docstring, hatch as mhatch, + lines as mlines, transforms) from .bezier import ( NonIntersectingPathException, get_cos_sin, get_intersection, get_parallels, inside_circle, make_wedged_bezier2, @@ -513,6 +514,8 @@ def set_hatch(self, hatch): ---------- hatch : {'/', '\\', '|', '-', '+', 'x', 'o', 'O', '.', '*'} """ + # Use validate_hatch(list) after deprecation. + mhatch._validate_hatch_pattern(hatch) self._hatch = hatch self.stale = True