diff --git a/lib/matplotlib/collections.py b/lib/matplotlib/collections.py index d0df32638be8..c182290e8fda 100644 --- a/lib/matplotlib/collections.py +++ b/lib/matplotlib/collections.py @@ -1808,9 +1808,10 @@ def __init__(self, patches, match_original=False, **kwargs): *match_original* If True, use the colors and linewidths of the original - patches. If False, new colors may be assigned by - providing the standard collection arguments, facecolor, - edgecolor, linewidths, norm or cmap. + patches. Also use the hatch of the first patch provided. + If False, new colors may be assigned by providing the + standard collection arguments, facecolor, edgecolor, + linewidths, norm or cmap. If any of *edgecolors*, *facecolors*, *linewidths*, *antialiaseds* are None, they default to their `.rcParams` patch setting, in sequence @@ -1827,12 +1828,22 @@ def determine_facecolor(patch): if patch.get_fill(): return patch.get_facecolor() return [0, 0, 0, 0] - kwargs['facecolors'] = [determine_facecolor(p) for p in patches] - kwargs['edgecolors'] = [p.get_edgecolor() for p in patches] + # If no edgecolor was set within the collection of patches + # then we will not supply the edgecolor argument, which allows the + # default hatch color to be used. + if any(p._original_edgecolor is not None for p in patches): + kwargs['edgecolors'] = [p.get_edgecolor() for p in patches] kwargs['linewidths'] = [p.get_linewidth() for p in patches] kwargs['linestyles'] = [p.get_linestyle() for p in patches] kwargs['antialiaseds'] = [p.get_antialiased() for p in patches] + if patches: + hatch = patches[0].get_hatch() + kwargs['hatch'] = hatch + if any(p.get_hatch() != hatch for p in patches): + warnings.warn("More than one type of hatch detected in " + "PatchCollection. Only using hatch of " + "first patch.") super().__init__(**kwargs) diff --git a/lib/matplotlib/tests/test_collections.py b/lib/matplotlib/tests/test_collections.py index acca60fd01c1..9e4888edbce0 100644 --- a/lib/matplotlib/tests/test_collections.py +++ b/lib/matplotlib/tests/test_collections.py @@ -10,6 +10,7 @@ from matplotlib.backend_bases import MouseEvent import matplotlib.collections as mcollections import matplotlib.colors as mcolors +import matplotlib.patches as mpatches import matplotlib.transforms as mtransforms from matplotlib.collections import (Collection, LineCollection, EventCollection, PolyCollection) @@ -1117,3 +1118,25 @@ def test_set_offset_units(): off0 = sc.get_offsets() sc.set_offsets(list(zip(y, d))) np.testing.assert_allclose(off0, sc.get_offsets()) + + +@check_figures_equal(extensions=['png']) +def test_patch_collection_keeps_hatch(fig_test, fig_ref): + # When creating a PatchCollection, the hatch of the original patches should + # be used if match_original == True. + ax = fig_test.subplots() + patches = [ + mpatches.Rectangle((0, 0), 0.2, .2, hatch="/"), + mpatches.Rectangle((0.4, 0.4), 0.2, .2, hatch="/") + ] + collection = mcollections.PatchCollection(patches, match_original=True) + ax.add_collection(collection) + + ax = fig_ref.subplots() + patches = [ + mpatches.Rectangle((0, 0), 0.2, .2), + mpatches.Rectangle((0.4, 0.4), 0.2, .2) + ] + collection = mcollections.PatchCollection(patches, match_original=False, + hatch="/") + ax.add_collection(collection) pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy