Skip to content

Commit e5454a3

Browse files
authored
Merge pull request #17926 from QuLogic/warn-hatch
Deprecate hatch patterns with invalid values
2 parents 38c41ae + 4da5274 commit e5454a3

File tree

4 files changed

+32
-4
lines changed

4 files changed

+32
-4
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
Invalid hatch pattern characters are no longer ignored
2+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
3+
4+
When specifying hatching patterns, characters that are not recognized will
5+
raise a DeprecationWarning. In the future, this will become a hard error.

lib/matplotlib/collections.py

+5-3
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515

1616
import matplotlib as mpl
1717
from . import (_path, artist, cbook, cm, colors as mcolors, docstring,
18-
lines as mlines, path as mpath, transforms)
18+
hatch as mhatch, lines as mlines, path as mpath, transforms)
1919
import warnings
2020

2121

@@ -141,8 +141,8 @@ def __init__(self,
141141
hatch : str, optional
142142
Hatching pattern to use in filled paths, if any. Valid strings are
143143
['/', '\\', '|', '-', '+', 'x', 'o', 'O', '.', '*']. See
144-
:doc:`/gallery/shapes_and_collections/hatch_demo` for the meaning
145-
of each hatch type.
144+
:doc:`/gallery/shapes_and_collections/hatch_style_reference` for
145+
the meaning of each hatch type.
146146
pickradius : float, default: 5.0
147147
If ``pickradius <= 0``, then `.Collection.contains` will return
148148
``True`` whenever the test point is inside of one of the polygons
@@ -522,6 +522,8 @@ def set_hatch(self, hatch):
522522
----------
523523
hatch : {'/', '\\', '|', '-', '+', 'x', 'o', 'O', '.', '*'}
524524
"""
525+
# Use validate_hatch(list) after deprecation.
526+
mhatch._validate_hatch_pattern(hatch)
525527
self._hatch = hatch
526528
self.stale = True
527529

lib/matplotlib/hatch.py

+18
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
"""Contains classes for generating hatch patterns."""
22

33
import numpy as np
4+
5+
from matplotlib import cbook
46
from matplotlib.path import Path
57

68

@@ -181,6 +183,22 @@ def __init__(self, hatch, density):
181183
]
182184

183185

186+
def _validate_hatch_pattern(hatch):
187+
valid_hatch_patterns = set(r'-+|/\xXoO.*')
188+
if hatch is not None:
189+
invalids = set(hatch).difference(valid_hatch_patterns)
190+
if invalids:
191+
valid = ''.join(sorted(valid_hatch_patterns))
192+
invalids = ''.join(sorted(invalids))
193+
cbook.warn_deprecated(
194+
'3.4',
195+
message=f'hatch must consist of a string of "{valid}" or '
196+
'None, but found the following invalid values '
197+
f'"{invalids}". Passing invalid values is deprecated '
198+
'since %(since)s and will become an error %(removal)s.'
199+
)
200+
201+
184202
def get_path(hatchpattern, density=6):
185203
"""
186204
Given a hatch specifier, *hatchpattern*, generates Path to render

lib/matplotlib/patches.py

+4-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,8 @@
88
import numpy as np
99

1010
import matplotlib as mpl
11-
from . import artist, cbook, colors, docstring, lines as mlines, transforms
11+
from . import (artist, cbook, colors, docstring, hatch as mhatch,
12+
lines as mlines, transforms)
1213
from .bezier import (
1314
NonIntersectingPathException, get_cos_sin, get_intersection,
1415
get_parallels, inside_circle, make_wedged_bezier2,
@@ -513,6 +514,8 @@ def set_hatch(self, hatch):
513514
----------
514515
hatch : {'/', '\\', '|', '-', '+', 'x', 'o', 'O', '.', '*'}
515516
"""
517+
# Use validate_hatch(list) after deprecation.
518+
mhatch._validate_hatch_pattern(hatch)
516519
self._hatch = hatch
517520
self.stale = True
518521

0 commit comments

Comments
 (0)