Skip to content

Changed hatch density typing to float. Added FutureWarning for cases … #27032

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

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
17 changes: 11 additions & 6 deletions lib/matplotlib/hatch.py
Original file line number Diff line number Diff line change
Expand Up @@ -133,15 +133,15 @@ class SmallCircles(Circles):
size = 0.2

def __init__(self, hatch, density):
self.num_rows = (hatch.count('o')) * density
self.num_rows = int((hatch.count('o')) * density)
super().__init__(hatch, density)


class LargeCircles(Circles):
size = 0.35

def __init__(self, hatch, density):
self.num_rows = (hatch.count('O')) * density
self.num_rows = int((hatch.count('O')) * density)
super().__init__(hatch, density)


Expand All @@ -150,7 +150,7 @@ class SmallFilledCircles(Circles):
filled = True

def __init__(self, hatch, density):
self.num_rows = (hatch.count('.')) * density
self.num_rows = int((hatch.count('.')) * density)
super().__init__(hatch, density)


Expand All @@ -159,7 +159,7 @@ class Stars(Shapes):
filled = True

def __init__(self, hatch, density):
self.num_rows = (hatch.count('*')) * density
self.num_rows = int((hatch.count('*')) * density)
path = Path.unit_regular_star(5)
self.shape_vertices = path.vertices
self.shape_codes = np.full(len(self.shape_vertices), Path.LINETO,
Expand Down Expand Up @@ -196,13 +196,18 @@ def _validate_hatch_pattern(hatch):
)


def get_path(hatchpattern, density=6):
def get_path(hatchpattern, density=6.0):
"""
Given a hatch specifier, *hatchpattern*, generates Path to render
the hatch in a unit square. *density* is the number of lines per
unit square.
"""
density = int(density)
if int(density) != density:
_api.warn_external("Passing a floating point density will result in "
"a behavior change due to float to int conversion."
f"Value density ({density}) will be used "
f"instead of {int(density)} to calculate lines",
category=FutureWarning)

patterns = [hatch_type(hatchpattern, density)
for hatch_type in _hatch_types]
Expand Down
22 changes: 11 additions & 11 deletions lib/matplotlib/hatch.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -8,61 +8,61 @@ class HatchPatternBase: ...
class HorizontalHatch(HatchPatternBase):
num_lines: int
num_vertices: int
def __init__(self, hatch: str, density: int) -> None: ...
def __init__(self, hatch: str, density: float) -> None: ...
def set_vertices_and_codes(self, vertices: ArrayLike, codes: ArrayLike) -> None: ...

class VerticalHatch(HatchPatternBase):
num_lines: int
num_vertices: int
def __init__(self, hatch: str, density: int) -> None: ...
def __init__(self, hatch: str, density: float) -> None: ...
def set_vertices_and_codes(self, vertices: ArrayLike, codes: ArrayLike) -> None: ...

class NorthEastHatch(HatchPatternBase):
num_lines: int
num_vertices: int
def __init__(self, hatch: str, density: int) -> None: ...
def __init__(self, hatch: str, density: float) -> None: ...
def set_vertices_and_codes(self, vertices: ArrayLike, codes: ArrayLike) -> None: ...

class SouthEastHatch(HatchPatternBase):
num_lines: int
num_vertices: int
def __init__(self, hatch: str, density: int) -> None: ...
def __init__(self, hatch: str, density: float) -> None: ...
def set_vertices_and_codes(self, vertices: ArrayLike, codes: ArrayLike) -> None: ...

class Shapes(HatchPatternBase):
filled: bool
num_shapes: int
num_vertices: int
def __init__(self, hatch: str, density: int) -> None: ...
def __init__(self, hatch: str, density: float) -> None: ...
def set_vertices_and_codes(self, vertices: ArrayLike, codes: ArrayLike) -> None: ...

class Circles(Shapes):
shape_vertices: np.ndarray
shape_codes: np.ndarray
def __init__(self, hatch: str, density: int) -> None: ...
def __init__(self, hatch: str, density: float) -> None: ...

class SmallCircles(Circles):
size: float
num_rows: int
def __init__(self, hatch: str, density: int) -> None: ...
def __init__(self, hatch: str, density: float) -> None: ...

class LargeCircles(Circles):
size: float
num_rows: int
def __init__(self, hatch: str, density: int) -> None: ...
def __init__(self, hatch: str, density: float) -> None: ...

class SmallFilledCircles(Circles):
size: float
filled: bool
num_rows: int
def __init__(self, hatch: str, density: int) -> None: ...
def __init__(self, hatch: str, density: float) -> None: ...

class Stars(Shapes):
size: float
filled: bool
num_rows: int
shape_vertices: np.ndarray
shape_codes: np.ndarray
def __init__(self, hatch: str, density: int) -> None: ...
def __init__(self, hatch: str, density: float) -> None: ...

def get_path(hatchpattern: str, density: int = ...) -> Path: ...
def get_path(hatchpattern: str, density: float = ...) -> Path: ...
2 changes: 1 addition & 1 deletion lib/matplotlib/path.py
Original file line number Diff line number Diff line change
Expand Up @@ -1024,7 +1024,7 @@ def wedge(cls, theta1, theta2, n=None):

@staticmethod
@lru_cache(8)
def hatch(hatchpattern, density=6):
def hatch(hatchpattern, density=6.0):
"""
Given a hatch specifier, *hatchpattern*, generates a `Path` that
can be used in a repeated hatching pattern. *density* is the
Expand Down
10 changes: 10 additions & 0 deletions lib/matplotlib/tests/test_path.py
Original file line number Diff line number Diff line change
Expand Up @@ -539,3 +539,13 @@ def test_cleanup_closepoly():
cleaned = p.cleaned(remove_nans=True)
assert len(cleaned) == 1
assert cleaned.codes[0] == Path.STOP


def test_hatch_density_integer_to_float():
with pytest.warns(FutureWarning,
match="behavior change due to float to int conversion."):
Path.hatch("x", 6.6)


def test_hatch_density_integer():
Path.hatch("x", 2.0)