8000 made hatches into a sequence for PathCollection · matplotlib/matplotlib@f62a8ea · GitHub
[go: up one dir, main page]

Skip to content

Commit f62a8ea

Browse files
committed
made hatches into a sequence for PathCollection
1 parent 85cbdc7 commit f62a8ea

File tree

3 files changed

+30
-44
lines changed

3 files changed

+30
-44
lines changed

lib/matplotlib/backend_bases.py

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -205,7 +205,7 @@ def draw_markers(self, gc, marker_path, marker_trans, path,
205205

206206
def draw_path_collection(self, gc, master_transform, paths, all_transforms,
207207
offsets, offset_trans, facecolors, edgecolors,
208-
linewidths, linestyles, antialiaseds, urls,
208+
linewidths, linestyles, antialiaseds, hatches, urls,
209209
offset_position):
210210
"""
211211
Draw a collection of *paths*.
@@ -236,7 +236,7 @@ def draw_path_collection(self, gc, master_transform, paths, all_transforms,
236236
for xo, yo, path_id, gc0, rgbFace in self._iter_collection(
237237
gc, list(path_ids), offsets, offset_trans,
238238
facecolors, edgecolors, linewidths, linestyles,
239-
antialiaseds, urls, offset_position):
239+
antialiaseds, hatches, urls, offset_position):
240240
path, transform = path_id
241241
# Only apply another translation if we have an offset, else we
242242
# reuse the initial transform.
@@ -335,7 +335,7 @@ def _iter_collection_uses_per_path(self, paths, all_transforms,
335335

336336
def _iter_collection(self, gc, path_ids, offsets, offset_trans, facecolors,
337337
edgecolors, linewidths, linestyles,
338-
antialiaseds, urls, offset_position):
338+
antialiaseds, hatches, urls, offset_position):
339339
"""
340340
Helper method (along with `_iter_collection_raw_paths`) to implement
341341
`draw_path_collection` in a memory-efficient manner.
@@ -366,6 +366,7 @@ def _iter_collection(self, gc, path_ids, offsets, offset_trans, facecolors,
366366
Nlinewidths = len(linewidths)
367367
Nlinestyles = len(linestyles)
368368
Nurls = len(urls)
369+
Nhatches = len(hatches)
369370

370371
if (Nfacecolors == 0 and Nedgecolors == 0) or Npaths == 0:
371372
return
@@ -386,12 +387,13 @@ def cycle_or_default(seq, default=None):
386387
lss = cycle_or_default(linestyles)
387388
aas = cycle_or_default(antialiaseds)
388389
urls = cycle_or_default(urls)
390+
hchs = cycle_or_default(hatches)
389391

390392
if Nedgecolors == 0:
391393
gc0.set_linewidth(0.0)
392394

393-
for pathid, (xo, yo), fc, ec, lw, ls, aa, url in itertools.islice(
394-
zip(pathids, toffsets, fcs, ecs, lws, lss, aas, urls), N):
395+
for pathid, (xo, yo), fc, ec, lw, ls, aa, hch, url in itertools.islice(
396+
zip(pathids, toffsets, fcs, ecs, lws, lss, aas, hchs, urls), N):
395397
if not (np.isfinite(xo) and np.isfinite(yo)):
396398
continue
397399
if Nedgecolors:
@@ -408,6 +410,8 @@ def cycle_or_default(seq, default=None):
408410
gc0.set_antialiased(aa)
409411
if Nurls:
410412
gc0.set_url(url)
413+
if Nhatches:
414+
gc0.set_hatch(hch)
411415
yield xo, yo, pathid, gc0, fc
412416
gc0.restore()
413417

lib/matplotlib/backends/backend_pdf.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2026,7 +2026,7 @@ def draw_path(self, gc, path, transform, rgbFace=None):
20262026

20272027
def draw_path_collection(self, gc, master_transform, paths, all_transforms,
20282028
offsets, offset_trans, facecolors, edgecolors,
2029-
linewidths, linestyles, antialiaseds, urls,
2029+
linewidths, linestyles, antialiaseds, hatches, urls,
20302030
offset_position):
20312031
# We can only reuse the objects if the presence of fill and
20322032
# stroke (and the amount of alpha for each) is the same for
@@ -2068,7 +2068,7 @@ def draw_path_collection(self, gc, master_transform, paths, all_transforms,
20682068
return RendererBase.draw_path_collection(
20692069
self, gc, master_transform, paths, all_transforms,
20702070
offsets, offset_trans, facecolors, edgecolors,
2071-
linewidths, linestyles, antialiaseds, urls,
2071+
linewidths, linestyles, antialiaseds, hatches, urls,
20722072
offset_position)
20732073

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

20902090
self.check_gc(gc0, rgbFace)
20912091
dx, dy = xo - lastx, yo - lasty

lib/matplotlib/collections.py

Lines changed: 18 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -171,7 +171,6 @@ def __init__(self, *,
171171
# Flags set by _set_mappable_flags: are colors from mapping an array?
172172
self._face_is_mapped = None
173173
self._edge_is_mapped = None
174-
self._match_original = False
175174
self._mapped_colors = None # calculated in update_scalarmappable
176175
self._hatch_color = mcolors.to_rgba(mpl.rcParams['hatch.color'])
177176
self.set_facecolor(facecolors)
@@ -362,7 +361,6 @@ def draw(self, renderer):
362361
gc.set_snap(self.get_snap())
363362

364363
if self._hatch:
365-
gc.set_hatch(self._hatch)
366364
gc.set_hatch_color(self._hatch_color)
367365

368366
if self.get_sketch_params() is not None:
@@ -387,7 +385,7 @@ def draw(self, renderer):
387385
len(self._linewidths) == 1 and
388386
all(ls[1] is None for ls in self._linestyles) and
389387
len(self._antialiaseds) == 1 and len(self._urls) == 1 and
390-
self.get_hatch() is None):
388+
len(self.get_hatch()) == 0):
391389
if len(trans):
392390
combined_transform = transforms.Affine2D(trans[0]) + transform
393391
else:
@@ -421,34 +419,16 @@ def draw(self, renderer):
421419
self.get_transforms(), offsets, offset_trf,
422420
[mcolors.to_rgba("none")], self._gapcolor,
423421
self._linewidths, ilinestyles,
424-
self._antialiaseds, self._urls,
422+
self._antialiaseds, self.get_hatch(), self._urls,
425423
"screen")
426424

427-
fcolor = itertools.cycle(facecolors) if facecolors.any() \
428-
else itertools.repeat([])
429-
ecolor = itertools.cycle(edgecolors) if edgecolors.any() \
430-
else itertools.repeat([])
431-
lwidth = itertools.cycle(self._linewidths)
432-
lstyle = itertools.cycle(self._linestyles)
433-
antialiased = itertools.cycle(self._antialiaseds)
434-
435-
if self._match_original:
436-
for idx in range(len(paths)):
437-
gc.set_hatch(self._hatch[idx])
438-
renderer.draw_path_collection(
439-
gc, transform.frozen(), [paths[idx]],
440-
self.get_transforms(), offsets, offset_trf,
441-
[next(fcolor)], [next(ecolor)], [next(lwidth)], [next(lstyle)],
442-
[next(antialiased)], self._urls,
443-
"screen") # offset_position, kept for backcompat.
444-
else:
445-
renderer.draw_path_collection(
446-
gc, transform.frozen(), paths,
447-
self.get_transforms(), offsets, offset_trf,
448-
self.get_facecolor(), self.get_edgecolor(),
449-
self._linewidths, self._linestyles,
450-
self._antialiaseds, self._urls,
451-
"screen")
425+
renderer.draw_path_collection(
426+
gc, transform.frozen(), paths,
427+
self.get_transforms(), offsets, offset_trf,
428+
self.get_facecolor(), self.get_edgecolor(),
429+
self._linewidths, self._linestyles,
430+
self._antialiaseds, self.get_hatch(), self._urls,
431+
"screen") # offset_position, kept for backcompat.
452432

453433
gc.restore()
454434
renderer.close_group(self.__class__.__name__)
@@ -552,7 +532,10 @@ def set_hatch(self, hatch):
552532
hatch : {'/', '\\', '|', '-', '+', 'x', 'o', 'O', '.', '*'}
553533
"""
554534
# Use validate_hatch(list) after deprecation.
555-
mhatch._validate_hatch_pattern(hatch)
535+
if isinstance(hatch, str):
536+
hatch = [hatch]
537+
for h in hatch:
538+
mhatch._validate_hatch_pattern(h)
556539
self._hatch = hatch
557540
self.stale = True
558541

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

18891872
if match_original:
1890-
self._match_original = True
1891-
kwargs['facecolors'] = tuple([p.get_facecolor() for p in patches])
1892-
kwargs['linewidths'] = tuple([p.get_linewidth() for p in patches])
1893-
kwargs['linestyles'] = tuple([p.get_linestyle() for p in patches])
1894-
kwargs['antialiaseds'] = tuple([p.get_antialiased() for p in patches])
1895-
kwargs['hatch'] = tuple([p.get_hatch() for p in patches])
1873+
kwargs['facecolors'] = [p.get_facecolor() for p in patches]
1874+
kwargs['linewidths'] = [p.get_linewidth() for p in patches]
1875+
kwargs['linestyles'] = [p.get_linestyle() for p in patches]
1876+
kwargs['antialiaseds'] = [p.get_antialiased() for p in patches]
1877+
kwargs['hatch'] = [p.get_hatch() for p in patches]
18961878

18971879
# Edgecolors are handled separately because are defaulted to None
18981880
# and the Hatch colors depend on them.

0 commit comments

Comments
 (0)
0