diff --git a/lib/matplotlib/patches.py b/lib/matplotlib/patches.py index 7e3aaf6ecb24..8e187aad318e 100644 --- a/lib/matplotlib/patches.py +++ b/lib/matplotlib/patches.py @@ -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 @@ -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: @@ -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) @@ -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): @@ -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) diff --git a/lib/matplotlib/patches.pyi b/lib/matplotlib/patches.pyi index f6c9ddf75839..1f258279545f 100644 --- a/lib/matplotlib/patches.pyi +++ b/lib/matplotlib/patches.pyi @@ -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: ... @@ -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: ... diff --git a/lib/matplotlib/tests/baseline_images/test_axes/bar_hatchcolor.pdf b/lib/matplotlib/tests/baseline_images/test_axes/bar_hatchcolor.pdf new file mode 100644 index 000000000000..dd6b9e2976dd Binary files /dev/null and b/lib/matplotlib/tests/baseline_images/test_axes/bar_hatchcolor.pdf differ diff --git a/lib/matplotlib/tests/baseline_images/test_axes/bar_hatchcolor.png b/lib/matplotlib/tests/baseline_images/test_axes/bar_hatchcolor.png new file mode 100644 index 000000000000..dd9ec62998fa Binary files /dev/null and b/lib/matplotlib/tests/baseline_images/test_axes/bar_hatchcolor.png differ diff --git a/lib/matplotlib/tests/baseline_images/test_axes/bar_hatchcolor_with_facecolor.pdf b/lib/matplotlib/tests/baseline_images/test_axes/bar_hatchcolor_with_facecolor.pdf new file mode 100644 index 000000000000..41529f51b7c2 Binary files /dev/null and b/lib/matplotlib/tests/baseline_images/test_axes/bar_hatchcolor_with_facecolor.pdf differ diff --git a/lib/matplotlib/tests/baseline_images/test_axes/bar_hatchcolor_with_facecolor.png b/lib/matplotlib/tests/baseline_images/test_axes/bar_hatchcolor_with_facecolor.png new file mode 100644 index 000000000000..796737ba4a70 Binary files /dev/null and b/lib/matplotlib/tests/baseline_images/test_axes/bar_hatchcolor_with_facecolor.png differ diff --git a/lib/matplotlib/tests/baseline_images/test_axes/bar_hatchcolor_with_facecolor_and_edgecolor.pdf b/lib/matplotlib/tests/baseline_images/test_axes/bar_hatchcolor_with_facecolor_and_edgecolor.pdf new file mode 100644 index 000000000000..effaf415cd96 Binary files /dev/null and b/lib/matplotlib/tests/baseline_images/test_axes/bar_hatchcolor_with_facecolor_and_edgecolor.pdf differ diff --git a/lib/matplotlib/tests/baseline_images/test_axes/bar_hatchcolor_with_facecolor_and_edgecolor.png b/lib/matplotlib/tests/baseline_images/test_axes/bar_hatchcolor_with_facecolor_and_edgecolor.png new file mode 100644 index 000000000000..15d1fef87d02 Binary files /dev/null and b/lib/matplotlib/tests/baseline_images/test_axes/bar_hatchcolor_with_facecolor_and_edgecolor.png differ diff --git a/lib/matplotlib/tests/test_axes.py b/lib/matplotlib/tests/test_axes.py index f2f74f845338..dd496d8101a2 100644 --- a/lib/matplotlib/tests/test_axes.py +++ b/lib/matplotlib/tests/test_axes.py @@ -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")