diff --git a/lib/matplotlib/contour.py b/lib/matplotlib/contour.py index 05fbedef2c68..8af7f6a15e19 100644 --- a/lib/matplotlib/contour.py +++ b/lib/matplotlib/contour.py @@ -1259,12 +1259,16 @@ def draw(self, renderer): super().draw(renderer) return # In presence of hatching, draw contours one at a time. + edgecolors = self.get_edgecolors() + if edgecolors.size == 0: + edgecolors = ("none",) for idx in range(n_paths): with cbook._setattr_cm(self, _paths=[paths[idx]]), self._cm_set( hatch=self.hatches[idx % len(self.hatches)], array=[self.get_array()[idx]], linewidths=[self.get_linewidths()[idx % len(self.get_linewidths())]], linestyles=[self.get_linestyles()[idx % len(self.get_linestyles())]], + edgecolors=edgecolors[idx % len(edgecolors)], ): super().draw(renderer) diff --git a/lib/matplotlib/tests/baseline_images/test_contour/contourf_hatch_colors.png b/lib/matplotlib/tests/baseline_images/test_contour/contourf_hatch_colors.png new file mode 100644 index 000000000000..18d949773ded Binary files /dev/null and b/lib/matplotlib/tests/baseline_images/test_contour/contourf_hatch_colors.png differ diff --git a/lib/matplotlib/tests/test_contour.py b/lib/matplotlib/tests/test_contour.py index 8ccff360a51a..e0ea82973af7 100644 --- a/lib/matplotlib/tests/test_contour.py +++ b/lib/matplotlib/tests/test_contour.py @@ -171,6 +171,14 @@ def test_given_colors_levels_and_extends(): plt.colorbar(c, ax=ax) +@image_comparison(['contourf_hatch_colors'], + remove_text=True, style='mpl20', extensions=['png']) +def test_hatch_colors(): + fig, ax = plt.subplots() + cf = ax.contourf([[0, 1], [1, 2]], hatches=['-', '/', '\\', '//'], cmap='gray') + cf.set_edgecolors(["blue", "grey", "yellow", "red"]) + + @pytest.mark.parametrize('color, extend', [('darkred', 'neither'), ('darkred', 'both'), (('r', 0.5), 'neither'),