Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Add support for match_original patch drawing
  • Loading branch information
Impaler343 committed Apr 17, 2024
commit a563671ebb6cc594eb9781ffc4b660bdfa1e9593
41 changes: 29 additions & 12 deletions lib/matplotlib/collections.py
Original file line number Diff line number Diff line change
Expand Up @@ -423,13 +423,31 @@ def draw(self, renderer):
self._antialiaseds, self._urls,
"screen")

renderer.draw_path_collection(
gc, transform.frozen(), paths,
self.get_transforms(), offsets, offset_trf,
self.get_facecolor(), self.get_edgecolor(),
self._linewidths, self._linestyles,
self._antialiaseds, self._urls,
"screen") # offset_position, kept for backcompat.
fcolor = itertools.cycle(facecolors) if facecolors.any() \
else itertools.repeat([])
ecolor = itertools.cycle(edgecolors) if edgecolors.any() \
else itertools.repeat([])
lwidth = itertools.cycle(self._linewidths)
lstyle = itertools.cycle(self._linestyles)
antialiased = itertools.cycle(self._antialiaseds)

if self._match_original:
for idx in range(len(paths)):
gc.set_hatch(self._hatch[idx])
renderer.draw_path_collection(
gc, transform.frozen(), [paths[idx]],
self.get_transforms(), offsets, offset_trf,
[next(fcolor)], [next(ecolor)], [next(lwidth)], [next(lstyle)],
[next(antialiased)], self._urls,
"screen") # offset_position, kept for backcompat.
else:
renderer.draw_path_collection(
gc, transform.frozen(), paths,
self.get_transforms(), offsets, offset_trf,
self.get_facecolor(), self.get_edgecolor(),

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

these return lists too, so why can't self.get_hatches, w/ changes as needed being implemented down in .draw_path_collection?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Makes sense

self._linewidths, self._linestyles,
self._antialiaseds, self._urls,
"screen")

gc.restore()
renderer.close_group(self.__class__.__name__)
Expand Down Expand Up @@ -1868,22 +1886,21 @@ def __init__(self, patches, *, match_original=False, **kwargs):
a call to `~.ScalarMappable.set_array`), at draw time a call to scalar
mappable will be made to set the face colors.
"""
self._match_original = False

if match_original:
self._match_original = True
kwargs['facecolors'] = [p.get_facecolor() 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]
kwargs['hatch'] = [p.get_hatch() for p in patches]

# Edgecolors are handled separately because are defaulted to None
# and the Hatch colors depend on them.
if any(p._original_edgecolor is not None for p in patches):
if all(p._original_edgecolor is not None for p in patches):
kwargs["edgecolors"] = [p.get_edgecolor() for p in patches]

# Using the hatch of only the first patch
if patches:
kwargs['hatch'] = patches[0].get_hatch()

super().__init__(**kwargs)

self.set_paths(patches)
Expand Down
2 changes: 1 addition & 1 deletion lib/matplotlib/hatch.py
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,7 @@ def __init__(self, hatch, density):


def _validate_hatch_pattern(hatch):
valid_hatch_patterns = set(r'-+|/\xXoO.*')
valid_hatch_patterns = set(r'-+|/\xXoO.*').union({None})

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why do you need to add None if next line you only validate if not none?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In the case of a list of Nones, the set function makes it into a singleton set with None. So it fails the

if hatch is not None:

condition

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok, but then I think you don't need the if hatch is not None anymore and can just go straight to validation

if hatch is not None:
invalids = set(hatch).difference(valid_hatch_patterns)
if invalids:
Expand Down