Skip to content
38 changes: 34 additions & 4 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
Comment thread
Vashesh08 marked this conversation as resolved.
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)
Comment thread
Vashesh08 marked this conversation as resolved.
self.set_edgecolor(edgecolor)
self.set_facecolor(facecolor)

Expand Down Expand Up @@ -312,17 +321,17 @@ 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
Comment thread
Vashesh08 marked this conversation as resolved.

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

Expand Down Expand Up @@ -369,8 +378,29 @@ 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):
self.set_hatch_color = True
Comment thread
Vashesh08 marked this conversation as resolved.
Outdated
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

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

Parameters
----------
c : color or None
"""
self._hatch_color = color
self._set_hatchcolor(color)
Comment thread
Vashesh08 marked this conversation as resolved.
Outdated

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) -> 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
37 changes: 37 additions & 0 deletions lib/matplotlib/tests/test_axes.py
Original file line number Diff line number Diff line change
Expand Up @@ -8861,3 +8861,40 @@ 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)


@check_figures_equal()
def test_bar_hatchcolor_with_facecolor_and_edgecolor(fig_test, fig_ref):

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

ax1 = fig_test.subplots()
ax1.bar(x, y, hatch="////", facecolor=(0, 0, 0, 0),
hatchcolor="red", edgecolor="black")

ax2 = fig_ref.subplots()
ax2.bar(x, y, hatch="////", facecolor=(0, 0, 0, 0),
edgecolor="black", hatchcolor="red")


@check_figures_equal()
def test_bar_hatchcolor_with_facecolor(fig_test, fig_ref):
x = [6, 7, 8, 9]
y = [2, 4, 7, 3]
ax1 = fig_test.subplots()
ax1.bar(x, y, hatch="////", hatchcolor="green", facecolor=(0, 0, 0, 0))

ax2 = fig_ref.subplots()
ax2.bar(x, y, hatch="////", facecolor=(0, 0, 0, 0), hatchcolor="green")


@check_figures_equal()
def test_bar_hatchcolor(fig_test, fig_ref):
x = [4, 7, 19, 8]
y = [1, 5, 8, 14]
ax1 = fig_test.subplots()
ax1.bar(x, y, hatch="////", hatchcolor="orange")

ax2 = fig_ref.subplots()
ax2.bar(x, y, hatch="////", hatchcolor="orange")
Comment thread
Vashesh08 marked this conversation as resolved.
Outdated