🌐 AI搜索 & 代理 主页
Skip to content

Commit f068c37

Browse files
committed
Changed hatch density typing to float. Added FutureWarning for cases where conversion would change behaviour
1 parent 4aa7efe commit f068c37

File tree

4 files changed

+33
-18
lines changed

4 files changed

+33
-18
lines changed

lib/matplotlib/hatch.py

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -133,15 +133,15 @@ class SmallCircles(Circles):
133133
size = 0.2
134134

135135
def __init__(self, hatch, density):
136-
self.num_rows = (hatch.count('o')) * density
136+
self.num_rows = int((hatch.count('o')) * density)
137137
super().__init__(hatch, density)
138138

139139

140140
class LargeCircles(Circles):
141141
size = 0.35
142142

143143
def __init__(self, hatch, density):
144-
self.num_rows = (hatch.count('O')) * density
144+
self.num_rows = int((hatch.count('O')) * density)
145145
super().__init__(hatch, density)
146146

147147

@@ -150,7 +150,7 @@ class SmallFilledCircles(Circles):
150150
filled = True
151151

152152
def __init__(self, hatch, density):
153-
self.num_rows = (hatch.count('.')) * density
153+
self.num_rows = int((hatch.count('.')) * density)
154154
super().__init__(hatch, density)
155155

156156

@@ -159,7 +159,7 @@ class Stars(Shapes):
159159
filled = True
160160

161161
def __init__(self, hatch, density):
162-
self.num_rows = (hatch.count('*')) * density
162+
self.num_rows = int((hatch.count('*')) * density)
163163
path = Path.unit_regular_star(5)
164164
self.shape_vertices = path.vertices
165165
self.shape_codes = np.full(len(self.shape_vertices), Path.LINETO,
@@ -196,13 +196,18 @@ def _validate_hatch_pattern(hatch):
196196
)
197197

198198

199-
def get_path(hatchpattern, density=6):
199+
def get_path(hatchpattern, density=6.0):
200200
"""
201201
Given a hatch specifier, *hatchpattern*, generates Path to render
202202
the hatch in a unit square. *density* is the number of lines per
203203
unit square.
204204
"""
205-
density = int(density)
205+
if int(density) != density:
206+
_api.warn_external("Passing a floating point density will result in "
207+
"a behavior change due to float to int conversion."
208+
f"Value density ({density}) will be used "
209+
f"instead of {int(density)} to calculate lines",
210+
category=FutureWarning)
206211

207212
patterns = [hatch_type(hatchpattern, density)
208213
for hatch_type in _hatch_types]

lib/matplotlib/hatch.pyi

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -8,61 +8,61 @@ class HatchPatternBase: ...
88
class HorizontalHatch(HatchPatternBase):
99
num_lines: int
1010
num_vertices: int
11-
def __init__(self, hatch: str, density: int) -> None: ...
11+
def __init__(self, hatch: str, density: float) -> None: ...
1212
def set_vertices_and_codes(self, vertices: ArrayLike, codes: ArrayLike) -> None: ...
1313

1414
class VerticalHatch(HatchPatternBase):
1515
num_lines: int
1616
num_vertices: int
17-
def __init__(self, hatch: str, density: int) -> None: ...
17+
def __init__(self, hatch: str, density: float) -> None: ...
1818
def set_vertices_and_codes(self, vertices: ArrayLike, codes: ArrayLike) -> None: ...
1919

2020
class NorthEastHatch(HatchPatternBase):
2121
num_lines: int
2222
num_vertices: int
23-
def __init__(self, hatch: str, density: int) -> None: ...
23+
def __init__(self, hatch: str, density: float) -> None: ...
2424
def set_vertices_and_codes(self, vertices: ArrayLike, codes: ArrayLike) -> None: ...
2525

2626
class SouthEastHatch(HatchPatternBase):
2727
num_lines: int
2828
num_vertices: int
29-
def __init__(self, hatch: str, density: int) -> None: ...
29+
def __init__(self, hatch: str, density: float) -> None: ...
3030
def set_vertices_and_codes(self, vertices: ArrayLike, codes: ArrayLike) -> None: ...
3131

3232
class Shapes(HatchPatternBase):
3333
filled: bool
3434
num_shapes: int
3535
num_vertices: int
36-
def __init__(self, hatch: str, density: int) -> None: ...
36+
def __init__(self, hatch: str, density: float) -> None: ...
3737
def set_vertices_and_codes(self, vertices: ArrayLike, codes: ArrayLike) -> None: ...
3838

3939
class Circles(Shapes):
4040
shape_vertices: np.ndarray
4141
shape_codes: np.ndarray
42-
def __init__(self, hatch: str, density: int) -> None: ...
42+
def __init__(self, hatch: str, density: float) -> None: ...
4343

4444
class SmallCircles(Circles):
4545
size: float
4646
num_rows: int
47-
def __init__(self, hatch: str, density: int) -> None: ...
47+
def __init__(self, hatch: str, density: float) -> None: ...
4848

4949
class LargeCircles(Circles):
5050
size: float
5151
num_rows: int
52-
def __init__(self, hatch: str, density: int) -> None: ...
52+
def __init__(self, hatch: str, density: float) -> None: ...
5353

5454
class SmallFilledCircles(Circles):
5555
size: float
5656
filled: bool
5757
num_rows: int
58-
def __init__(self, hatch: str, density: int) -> None: ...
58+
def __init__(self, hatch: str, density: float) -> None: ...
5959

6060
class Stars(Shapes):
6161
size: float
6262
filled: bool
6363
num_rows: int
6464
shape_vertices: np.ndarray
6565
shape_codes: np.ndarray
66-
def __init__(self, hatch: str, density: int) -> None: ...
66+
def __init__(self, hatch: str, density: float) -> None: ...
6767

68-
def get_path(hatchpattern: str, density: int = ...) -> Path: ...
68+
def get_path(hatchpattern: str, density: float = ...) -> Path: ...

lib/matplotlib/path.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1024,7 +1024,7 @@ def wedge(cls, theta1, theta2, n=None):
10241024

10251025
@staticmethod
10261026
@lru_cache(8)
1027-
def hatch(hatchpattern, density=6):
1027+
def hatch(hatchpattern, density=6.0):
10281028
"""
10291029
Given a hatch specifier, *hatchpattern*, generates a `Path` that
10301030
can be used in a repeated hatching pattern. *density* is the

lib/matplotlib/tests/test_path.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -539,3 +539,13 @@ def test_cleanup_closepoly():
539539
cleaned = p.cleaned(remove_nans=True)
540540
assert len(cleaned) == 1
541541
assert cleaned.codes[0] == Path.STOP
542+
543+
544+
def test_hatch_density_integer_to_float():
545+
with pytest.warns(FutureWarning,
546+
match="behavior change due to float to int conversion."):
547+
Path.hatch("x", 6.6)
548+
549+
550+
def test_hatch_density_integer():
551+
Path.hatch("x", 2.0)

0 commit comments

Comments
 (0)