diff --git a/lib/matplotlib/legend_handler.py b/lib/matplotlib/legend_handler.py index 1cc81fa0474a..79661119db31 100644 --- a/lib/matplotlib/legend_handler.py +++ b/lib/matplotlib/legend_handler.py @@ -592,6 +592,8 @@ class HandlerPolyCollection(HandlerBase): """ def _update_prop(self, legend_handle, orig_handle): def first_color(colors): + if colors is None: + return None colors = mcolors.to_rgba_array(colors) if len(colors): return colors[0] @@ -602,8 +604,12 @@ def get_first(prop_array): return prop_array[0] else: return None - legend_handle.set_edgecolor(first_color(orig_handle.get_edgecolor())) - legend_handle.set_facecolor(first_color(orig_handle.get_facecolor())) + edgecolor = getattr(orig_handle, '_original_edgecolor', + orig_handle.get_edgecolor()) + legend_handle.set_edgecolor(first_color(edgecolor)) + facecolor = getattr(orig_handle, '_original_facecolor', + orig_handle.get_facecolor()) + legend_handle.set_facecolor(first_color(facecolor)) legend_handle.set_fill(orig_handle.get_fill()) legend_handle.set_hatch(orig_handle.get_hatch()) legend_handle.set_linewidth(get_first(orig_handle.get_linewidths())) diff --git a/lib/matplotlib/patches.py b/lib/matplotlib/patches.py index 2cb21986f8d5..bb52b189329e 100644 --- a/lib/matplotlib/patches.py +++ b/lib/matplotlib/patches.py @@ -200,6 +200,7 @@ def update_from(self, other): self._facecolor = other._facecolor self._fill = other._fill self._hatch = other._hatch + self._hatch_color = other._hatch_color # copy the unscaled dash pattern self._us_dashes = other._us_dashes self.set_linewidth(other._linewidth) # also sets dash properties diff --git a/lib/matplotlib/tests/baseline_images/test_legend/hatching.pdf b/lib/matplotlib/tests/baseline_images/test_legend/hatching.pdf new file mode 100644 index 000000000000..e345dbff7ce5 Binary files /dev/null and b/lib/matplotlib/tests/baseline_images/test_legend/hatching.pdf differ diff --git a/lib/matplotlib/tests/baseline_images/test_legend/hatching.png b/lib/matplotlib/tests/baseline_images/test_legend/hatching.png new file mode 100644 index 000000000000..9a309c98bb91 Binary files /dev/null and b/lib/matplotlib/tests/baseline_images/test_legend/hatching.png differ diff --git a/lib/matplotlib/tests/baseline_images/test_legend/hatching.svg b/lib/matplotlib/tests/baseline_images/test_legend/hatching.svg new file mode 100644 index 000000000000..a662f64f20a4 --- /dev/null +++ b/lib/matplotlib/tests/baseline_images/test_legend/hatching.svg @@ -0,0 +1,1022 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/lib/matplotlib/tests/test_legend.py b/lib/matplotlib/tests/test_legend.py index 052e4959c968..d8dd6b979b9a 100644 --- a/lib/matplotlib/tests/test_legend.py +++ b/lib/matplotlib/tests/test_legend.py @@ -157,6 +157,37 @@ def test_legend_expand(): ax.legend(loc=3, mode=mode, ncol=2) +@image_comparison(baseline_images=['hatching'], remove_text=True, + style='default') +def test_hatching(): + fig, ax = plt.subplots() + + # Patches + patch = plt.Rectangle((0, 0), 0.3, 0.3, hatch='xx', + label='Patch\ndefault color\nfilled') + ax.add_patch(patch) + patch = plt.Rectangle((0.33, 0), 0.3, 0.3, hatch='||', edgecolor='C1', + label='Patch\nexplicit color\nfilled') + ax.add_patch(patch) + patch = plt.Rectangle((0, 0.4), 0.3, 0.3, hatch='xx', fill=False, + label='Patch\ndefault color\nunfilled') + ax.add_patch(patch) + patch = plt.Rectangle((0.33, 0.4), 0.3, 0.3, hatch='||', fill=False, + edgecolor='C1', + label='Patch\nexplicit color\nunfilled') + ax.add_patch(patch) + + # Paths + ax.fill_between([0, .15, .3], [.8, .8, .8], [.9, 1.0, .9], + hatch='+', label='Path\ndefault color') + ax.fill_between([.33, .48, .63], [.8, .8, .8], [.9, 1.0, .9], + hatch='+', edgecolor='C2', label='Path\nexplicit color') + + ax.set_xlim(-0.01, 1.1) + ax.set_ylim(-0.01, 1.1) + ax.legend(handlelength=4, handleheight=4) + + @cleanup def test_legend_remove(): fig = plt.figure()