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
made hatches into a sequence for PathCollection
  • Loading branch information
r3kste committed Aug 15, 2024
commit f62a8ea0e86b9fab354e11303915a42d1d8d89b2
14 changes: 9 additions & 5 deletions lib/matplotlib/backend_bases.py
Original file line number Diff line number Diff line change
Expand Up @@ -205,7 +205,7 @@ def draw_markers(self, gc, marker_path, marker_trans, path,

def draw_path_collection(self, gc, master_transform, paths, all_transforms,
offsets, offset_trans, facecolors, edgecolors,
linewidths, linestyles, antialiaseds, urls,
linewidths, linestyles, antialiaseds, hatches, urls,
offset_position):
"""
Draw a collection of *paths*.
Expand Down Expand Up @@ -236,7 +236,7 @@ def draw_path_collection(self, gc, master_transform, paths, all_transforms,
for xo, yo, path_id, gc0, rgbFace in self._iter_collection(
gc, list(path_ids), offsets, offset_trans,
facecolors, edgecolors, linewidths, linestyles,
antialiaseds, urls, offset_position):
antialiaseds, hatches, urls, offset_position):
path, transform = path_id
# Only apply another translation if we have an offset, else we
# reuse the initial transform.
Expand Down Expand Up @@ -335,7 +335,7 @@ def _iter_collection_uses_per_path(self, paths, all_transforms,

def _iter_collection(self, gc, path_ids, offsets, offset_trans, facecolors,
edgecolors, linewidths, linestyles,
antialiaseds, urls, offset_position):
antialiaseds, hatches, urls, offset_position):
"""
Helper method (along with `_iter_collection_raw_paths`) to implement
`draw_path_collection` in a memory-efficient manner.
Expand Down Expand Up @@ -366,6 +366,7 @@ def _iter_collection(self, gc, path_ids, offsets, offset_trans, facecolors,
Nlinewidths = len(linewidths)
Nlinestyles = len(linestyles)
Nurls = len(urls)
Nhatches = len(hatches)

if (Nfacecolors == 0 and Nedgecolors == 0) or Npaths == 0:
return
Expand All @@ -386,12 +387,13 @@ def cycle_or_default(seq, default=None):
lss = cycle_or_default(linestyles)
aas = cycle_or_default(antialiaseds)
urls = cycle_or_default(urls)
hchs = cycle_or_default(hatches)

if Nedgecolors == 0:
gc0.set_linewidth(0.0)

for pathid, (xo, yo), fc, ec, lw, ls, aa, url in itertools.islice(
zip(pathids, toffsets, fcs, ecs, lws, lss, aas, urls), N):
for pathid, (xo, yo), fc, ec, lw, ls, aa, hch, url in itertools.islice(
zip(pathids, toffsets, fcs, ecs, lws, lss, aas, hchs, urls), N):
if not (np.isfinite(xo) and np.isfinite(yo)):
continue
if Nedgecolors:
Expand All @@ -408,6 +410,8 @@ def cycle_or_default(seq, default=None):
gc0.set_antialiased(aa)
if Nurls:
gc0.set_url(url)
if Nhatches:
gc0.set_hatch(hch)
yield xo, yo, pathid, gc0, fc
gc0.restore()

Expand Down
6 changes: 3 additions & 3 deletions lib/matplotlib/backends/backend_pdf.py
Original file line number Diff line number Diff line change
Expand Up @@ -2026,7 +2026,7 @@ def draw_path(self, gc, path, transform, rgbFace=None):

def draw_path_collection(self, gc, master_transform, paths, all_transforms,
offsets, offset_trans, facecolors, edgecolors,
linewidths, linestyles, antialiaseds, urls,
linewidths, linestyles, antialiaseds, hatches, urls,
offset_position):
# We can only reuse the objects if the presence of fill and
# stroke (and the amount of alpha for each) is the same for
Expand Down Expand Up @@ -2068,7 +2068,7 @@ def draw_path_collection(self, gc, master_transform, paths, all_transforms,
return RendererBase.draw_path_collection(
self, gc, master_transform, paths, all_transforms,
offsets, offset_trans, facecolors, edgecolors,
linewidths, linestyles, antialiaseds, urls,
linewidths, linestyles, antialiaseds, hatches, urls,
offset_position)

padding = np.max(linewidths)
Expand All @@ -2085,7 +2085,7 @@ def draw_path_collection(self, gc, master_transform, paths, all_transforms,
for xo, yo, path_id, gc0, rgbFace in self._iter_collection(
gc, path_codes, offsets, offset_trans,
facecolors, edgecolors, linewidths, linestyles,
antialiaseds, urls, offset_position):
antialiaseds, hatches, urls, offset_position):

self.check_gc(gc0, rgbFace)
dx, dy = xo - lastx, yo - lasty
Expand Down
54 changes: 18 additions & 36 deletions lib/matplotlib/collections.py
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,6 @@ def __init__(self, *,
# Flags set by _set_mappable_flags: are colors from mapping an array?
self._face_is_mapped = None
self._edge_is_mapped = None
self._match_original = False
self._mapped_colors = None # calculated in update_scalarmappable
self._hatch_color = mcolors.to_rgba(mpl.rcParams['hatch.color'])
self.set_facecolor(facecolors)
Expand Down Expand Up @@ -362,7 +361,6 @@ def draw(self, renderer):
gc.set_snap(self.get_snap())

if self._hatch:
gc.set_hatch(self._hatch)
gc.set_hatch_color(self._hatch_color)

if self.get_sketch_params() is not None:
Expand All @@ -387,7 +385,7 @@ def draw(self, renderer):
len(self._linewidths) == 1 and
all(ls[1] is None for ls in self._linestyles) and
len(self._antialiaseds) == 1 and len(self._urls) == 1 and
self.get_hatch() is None):
len(self.get_hatch()) == 0):
if len(trans):
combined_transform = transforms.Affine2D(trans[0]) + transform
else:
Expand Down Expand Up @@ -421,34 +419,16 @@ def draw(self, renderer):
self.get_transforms(), offsets, offset_trf,
[mcolors.to_rgba("none")], self._gapcolor,
self._linewidths, ilinestyles,
self._antialiaseds, self._urls,
self._antialiaseds, self.get_hatch(), self._urls,
"screen")

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(),
self._linewidths, self._linestyles,
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.get_hatch(), self._urls,
"screen") # offset_position, kept for backcompat.

gc.restore()
renderer.close_group(self.__class__.__name__)
Expand Down Expand Up @@ -552,7 +532,10 @@ def set_hatch(self, hatch):
hatch : {'/', '\\', '|', '-', '+', 'x', 'o', 'O', '.', '*'}
"""
# Use validate_hatch(list) after deprecation.
mhatch._validate_hatch_pattern(hatch)
if isinstance(hatch, str):
hatch = [hatch]
for h in hatch:
mhatch._validate_hatch_pattern(h)
self._hatch = hatch
self.stale = True

Expand Down Expand Up @@ -1887,12 +1870,11 @@ def __init__(self, patches, *, match_original=False, **kwargs):
"""

if match_original:
self._match_original = True
kwargs['facecolors'] = tuple([p.get_facecolor() for p in patches])
kwargs['linewidths'] = tuple([p.get_linewidth() for p in patches])
kwargs['linestyles'] = tuple([p.get_linestyle() for p in patches])
kwargs['antialiaseds'] = tuple([p.get_antialiased() for p in patches])
kwargs['hatch'] = tuple([p.get_hatch() for p in patches])
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.
Expand Down