Skip to content

Resolves different edgecolor and hatch color in bar plot #26993

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

Closed
wants to merge 11 commits into from
44 changes: 39 additions & 5 deletions lib/matplotlib/patches.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,16 @@ def __init__(self, *,
fill=True,
capstyle=None,
joinstyle=None,
hatchcolor=None,
**kwargs):
"""
Parameters
----------

hatchcolor : color or list of color, optional
The color of the hatch inside the bar
.. versionadded:: 3.9

The following kwarg properties are supported

%(Patch:kwdoc)s
Expand All @@ -71,7 +79,7 @@ def __init__(self, *,
if joinstyle is None:
joinstyle = JoinStyle.miter

self._hatch_color = colors.to_rgba(mpl.rcParams['hatch.color'])
self.set_hatch_color = False
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 @@ -80,6 +88,7 @@ def __init__(self, *,
"the edgecolor or facecolor properties.")
self.set_color(color)
else:
self.set_hatchcolor(hatchcolor)
self.set_edgecolor(edgecolor)
self.set_facecolor(facecolor)

Expand Down Expand Up @@ -312,18 +321,18 @@ def set_antialiased(self, aa):
self.stale = True

def _set_edgecolor(self, color):
set_hatch_color = True
set_hatch_color_from_edgecolor = 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
set_hatch_color_from_edgecolor = False

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

def set_edgecolor(self, color):
Expand Down Expand Up @@ -369,8 +378,33 @@ def set_color(self, c):
For setting the edge or face color individually.
"""
self.set_facecolor(c)
self.set_hatchcolor(c)
self.set_edgecolor(c)

def _set_hatchcolor(self, color, set_hatchcolor_from_edgecolor=False):
if not set_hatchcolor_from_edgecolor:
self.set_hatch_color = True
if color is None:
self.set_hatch_color = False
color = mpl.rcParams['hatch.color']
alpha = self._alpha if self._fill else 0
self._hatch_color = colors.to_rgba(color, alpha)
self.stale = True

if not self.set_hatch_color:
self._hatch_color = colors.to_rgba(color, self._alpha)

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

Parameters
----------
c : color or None
"""
self._original_hatchcolor = color
self._set_hatchcolor(color, set_hatchcolor_from_edgecolor)

def set_alpha(self, alpha):
# docstring inherited
super().set_alpha(alpha)
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, set_hatchcolor_from_edgecolor=False) -> 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
Binary file not shown.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file not shown.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file not shown.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
28 changes: 28 additions & 0 deletions lib/matplotlib/tests/test_axes.py
Original file line number Diff line number Diff line change
Expand Up @@ -8899,3 +8899,31 @@ def test_axhvlinespan_interpolation():
ax.axhline(1, c="C0", alpha=.5)
ax.axhspan(.8, .9, fc="C1", alpha=.5)
ax.axhspan(.6, .7, .8, .9, fc="C2", alpha=.5)


@image_comparison(["bar_hatchcolor_with_facecolor_and_edgecolor"],
extensions=["png", "pdf"])
def test_bar_hatchcolor_with_facecolor_and_edgecolor():

x = [2, 3, 4, 5]
y = [1, 3, 1, 4]

fig, ax = plt.subplots()
ax.bar(x, y, hatch="////", facecolor="yellow",
hatchcolor="red", edgecolor="blue")


@image_comparison(["bar_hatchcolor_with_facecolor"], extensions=["png", "pdf"])
def test_bar_hatchcolor_with_facecolor():
x = [6, 7, 8, 9]
y = [2, 4, 7, 3]
fig, ax = plt.subplots()
ax.bar(x, y, hatch="////", hatchcolor="green", facecolor="red")


@image_comparison(["bar_hatchcolor"], extensions=["png", "pdf"])
def test_bar_hatchcolor():
x = [4, 7, 19, 8]
y = [1, 5, 8, 14]
fig, ax = plt.subplots()
ax.bar(x, y, hatch="////", hatchcolor="orange")