Skip to content
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

Seperates edgecolor from hatchcolor #28104

Draft
wants to merge 9 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
6 changes: 5 additions & 1 deletion lib/matplotlib/backend_bases.py
Original file line number Diff line number Diff line change
Expand Up @@ -735,7 +735,11 @@ def __init__(self):
self._linewidth = 1
self._rgb = (0.0, 0.0, 0.0, 1.0)
self._hatch = None
self._hatch_color = colors.to_rgba(rcParams['hatch.color'])
self._hatch_color = colors.to_rgba(
rcParams["hatch.color"]
if rcParams["hatch.color"] != "inherit"
else rcParams["patch.edgecolor"]
)
self._hatch_linewidth = rcParams['hatch.linewidth']
self._url = None
self._gid = None
Expand Down
38 changes: 33 additions & 5 deletions lib/matplotlib/collections.py
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@
def __init__(self, *,
edgecolors=None,
facecolors=None,
hatchcolors=None,
linewidths=None,
linestyles='solid',
capstyle=None,
Expand Down Expand Up @@ -172,7 +173,11 @@
self._face_is_mapped = None
self._edge_is_mapped = None
self._mapped_colors = None # calculated in update_scalarmappable
self._hatch_color = mcolors.to_rgba(mpl.rcParams['hatch.color'])
hatchcolors = mpl._val_or_rc(hatchcolors, "hatch.color")
if isinstance(hatchcolors, str):
self._hatch_color = mcolors.to_rgba(hatchcolors)
else:
self._hatch_color = mcolors.to_rgba_array(hatchcolors[0])

Check warning on line 180 in lib/matplotlib/collections.py

View check run for this annotation

Codecov / codecov/patch

lib/matplotlib/collections.py#L180

Added line #L180 was not covered by tests
self.set_facecolor(facecolors)
self.set_edgecolor(edgecolors)
self.set_linewidth(linewidths)
Expand All @@ -181,6 +186,7 @@
self.set_pickradius(pickradius)
self.set_urls(urls)
self.set_hatch(hatch)
self.set_hatchcolor(hatchcolors)
self.set_zorder(zorder)

if capstyle:
Expand Down Expand Up @@ -797,22 +803,18 @@
return mpl.rcParams['patch.edgecolor']

def _set_edgecolor(self, c):
set_hatch_color = True
if c is None:
if (mpl.rcParams['patch.force_edgecolor']
or self._edge_default
or cbook._str_equal(self._original_facecolor, 'none')):
c = self._get_default_edgecolor()
else:
c = 'none'
set_hatch_color = False
if cbook._str_lower_equal(c, 'face'):
self._edgecolors = 'face'
self.stale = True
return
self._edgecolors = mcolors.to_rgba_array(c, self._alpha)
if set_hatch_color and len(self._edgecolors):
self._hatch_color = tuple(self._edgecolors[0])
self.stale = True

def set_edgecolor(self, c):
Expand All @@ -833,6 +835,30 @@
self._original_edgecolor = c
self._set_edgecolor(c)

def _set_hatchcolor(self, c):
if cbook._str_lower_equal(c, 'inherit'):
self._hatchcolors = 'inherit'
self.stale = True
return

Check warning on line 842 in lib/matplotlib/collections.py

View check run for this annotation

Codecov / codecov/patch

lib/matplotlib/collections.py#L840-L842

Added lines #L840 - L842 were not covered by tests
self._hatchcolors = mcolors.to_rgba_array(c, self._alpha)
self._hatch_color = self._hatchcolors[0] # Waiting for PR #27937
self.stale = True

def set_hatchcolor(self, c):
"""
Set the hatchcolor(s) of the collection.

Parameters
----------
c : :mpltype:`color` or list of :mpltype:`color` or 'inherit'
The collection hatchcolor(s). If a sequence, the patches cycle
through it. If 'face', match the facecolor.
"""
if isinstance(c, str) and c.lower() in ("none", "inherit"):
c = c.lower()

Check warning on line 858 in lib/matplotlib/collections.py

View check run for this annotation

Codecov / codecov/patch

lib/matplotlib/collections.py#L858

Added line #L858 was not covered by tests
self._original_hatchcolor = c
self._set_hatchcolor(c)

def set_alpha(self, alpha):
"""
Set the transparency of the collection.
Expand All @@ -848,6 +874,7 @@
artist.Artist._set_alpha_for_array(self, alpha)
self._set_facecolor(self._original_facecolor)
self._set_edgecolor(self._original_edgecolor)
self._set_hatchcolor(self._original_hatchcolor)

set_alpha.__doc__ = artist.Artist._set_alpha_for_array.__doc__

Expand Down Expand Up @@ -950,6 +977,7 @@
self._us_linestyles = other._us_linestyles
self._pickradius = other._pickradius
self._hatch = other._hatch
self._hatch_color = other._hatch_color

# update_from for scalarmappable
self._A = other._A
Expand Down
2 changes: 2 additions & 0 deletions lib/matplotlib/collections.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ class Collection(artist.Artist, cm.ScalarMappable):
*,
edgecolors: ColorType | Sequence[ColorType] | None = ...,
facecolors: ColorType | Sequence[ColorType] | None = ...,
hatchcolors: ColorType | Sequence[ColorType] | None = ...,
linewidths: float | Sequence[float] | None = ...,
linestyles: LineStyleType | Sequence[LineStyleType] = ...,
capstyle: CapStyleType | None = ...,
Expand Down Expand Up @@ -63,6 +64,7 @@ class Collection(artist.Artist, cm.ScalarMappable):
def get_facecolor(self) -> ColorType | Sequence[ColorType]: ...
def get_edgecolor(self) -> ColorType | Sequence[ColorType]: ...
def set_edgecolor(self, c: ColorType | Sequence[ColorType]) -> None: ...
def set_hatchcolor(self, c: ColorType | Sequence[ColorType]) -> None: ...
def set_alpha(self, alpha: float | Sequence[float] | None) -> None: ...
def get_linewidth(self) -> float | Sequence[float]: ...
def get_linestyle(self) -> LineStyleType | Sequence[LineStyleType]: ...
Expand Down
2 changes: 1 addition & 1 deletion lib/matplotlib/mpl-data/matplotlibrc
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,7 @@
## ***************************************************************************
## * HATCHES *
## ***************************************************************************
#hatch.color: black
#hatch.color: inherit
#hatch.linewidth: 1.0


Expand Down
32 changes: 26 additions & 6 deletions lib/matplotlib/patches.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ def __init__(self, *,
fill=True,
capstyle=None,
joinstyle=None,
hatchcolor=None,
**kwargs):
"""
The following kwarg properties are supported
Expand All @@ -71,7 +72,10 @@ def __init__(self, *,
if joinstyle is None:
joinstyle = JoinStyle.miter

self._hatch_color = colors.to_rgba(mpl.rcParams['hatch.color'])
hatchcolor = mpl._val_or_rc(hatchcolor, "hatch.color")
if hatchcolor == 'inherit':
hatchcolor = mpl.rcParams["patch.edgecolor"]

self._fill = bool(fill) # needed for set_facecolor call
if color is not None:
if edgecolor is not None or facecolor is not None:
Expand All @@ -81,6 +85,7 @@ def __init__(self, *,
self.set_color(color)
else:
self.set_edgecolor(edgecolor)
self.set_hatchcolor(hatchcolor)
self.set_facecolor(facecolor)

self._linewidth = 0
Expand Down Expand Up @@ -290,6 +295,7 @@ def update_from(self, other):
self._fill = other._fill
self._hatch = other._hatch
self._hatch_color = other._hatch_color
self._original_hatchcolor = other._original_hatchcolor
self._unscaled_dash_pattern = other._unscaled_dash_pattern
self.set_linewidth(other._linewidth) # also sets scaled dashes
self.set_transform(other.get_data_transform())
Expand Down Expand Up @@ -359,18 +365,14 @@ def set_antialiased(self, aa):
self.stale = True

def _set_edgecolor(self, color):
set_hatch_color = True
if color is None:
if (mpl.rcParams['patch.force_edgecolor'] or
not self._fill or self._edge_default):
color = mpl.rcParams['patch.edgecolor']
else:
color = 'none'
set_hatch_color = False

self._edgecolor = colors.to_rgba(color, self._alpha)
if set_hatch_color:
self._hatch_color = self._edgecolor
self.stale = True

def set_edgecolor(self, color):
Expand Down Expand Up @@ -415,14 +417,31 @@ def set_color(self, c):
Patch.set_facecolor, Patch.set_edgecolor
For setting the edge or face color individually.
"""
self.set_facecolor(c)
self.set_edgecolor(c)
self.set_hatchcolor(c)
self.set_facecolor(c)

def _set_hatchcolor(self, color):
self._hatch_color = colors.to_rgba(color)
self.stale = True

def set_hatchcolor(self, color):
"""
Set the patch hatch color.

Parameters
----------
color : :mpltype:`color` or None
"""
self._original_hatchcolor = color
self._set_hatchcolor(color)

def set_alpha(self, alpha):
# docstring inherited
super().set_alpha(alpha)
self._set_facecolor(self._original_facecolor)
self._set_edgecolor(self._original_edgecolor)
self._set_hatchcolor(self._original_hatchcolor)
# stale is already True

def set_linewidth(self, w):
Expand Down Expand Up @@ -486,6 +505,7 @@ def set_fill(self, b):
self._fill = bool(b)
self._set_facecolor(self._original_facecolor)
self._set_edgecolor(self._original_edgecolor)
self._set_hatchcolor(self._original_hatchcolor)
self.stale = True

def get_fill(self):
Expand Down
2 changes: 2 additions & 0 deletions lib/matplotlib/patches.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ class Patch(artist.Artist):
fill: bool = ...,
capstyle: CapStyleType | None = ...,
joinstyle: JoinStyleType | None = ...,
hatchcolor: ColorType | None = ...,
**kwargs,
) -> None: ...
def get_verts(self) -> ArrayLike: ...
Expand All @@ -48,6 +49,7 @@ class Patch(artist.Artist):
def set_edgecolor(self, color: ColorType | None) -> None: ...
def set_facecolor(self, color: ColorType | None) -> None: ...
def set_color(self, c: ColorType | None) -> None: ...
def set_hatchcolor(self, color: ColorType | None) -> None: ...
def set_alpha(self, alpha: float | None) -> None: ...
def set_linewidth(self, w: float | None) -> None: ...
def set_linestyle(self, ls: LineStyleType | None) -> None: ...
Expand Down
2 changes: 1 addition & 1 deletion lib/matplotlib/rcsetup.py
Original file line number Diff line number Diff line change
Expand Up @@ -963,7 +963,7 @@ def _convert_validator_spec(key, conv):
"patch.antialiased": validate_bool, # antialiased (no jaggies)

## hatch props
"hatch.color": validate_color,
"hatch.color": validate_color_or_inherit,
"hatch.linewidth": validate_float,

## Histogram properties
Expand Down