Skip to content

Commit aa5fdbb

Browse files
authored
Merge pull request #21696 from anntzer/rendererbase_iter_collection
Use cycling iterators in RendererBase.
2 parents 1a74793 + 35c4b07 commit aa5fdbb

File tree

1 file changed

+26
-29
lines changed

1 file changed

+26
-29
lines changed

lib/matplotlib/backend_bases.py

Lines changed: 26 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
import importlib
3333
import inspect
3434
import io
35+
import itertools
3536
import logging
3637
import os
3738
import sys
@@ -388,61 +389,57 @@ def _iter_collection(self, gc, master_transform, all_transforms,
388389
*path_ids*; *gc* is a graphics context and *rgbFace* is a color to
389390
use for filling the path.
390391
"""
391-
Ntransforms = len(all_transforms)
392392
Npaths = len(path_ids)
393393
Noffsets = len(offsets)
394394
N = max(Npaths, Noffsets)
395395
Nfacecolors = len(facecolors)
396396
Nedgecolors = len(edgecolors)
397397
Nlinewidths = len(linewidths)
398398
Nlinestyles = len(linestyles)
399-
Naa = len(antialiaseds)
400399
Nurls = len(urls)
401400

402401
if (Nfacecolors == 0 and Nedgecolors == 0) or Npaths == 0:
403402
return
404-
if Noffsets:
405-
toffsets = offsetTrans.transform(offsets)
406403

407404
gc0 = self.new_gc()
408405
gc0.copy_properties(gc)
409406

410-
if Nfacecolors == 0:
411-
rgbFace = None
407+
def cycle_or_default(seq, default=None):
408+
# Cycle over *seq* if it is not empty; else always yield *default*.
409+
return (itertools.cycle(seq) if len(seq)
410+
else itertools.repeat(default))
411+
412+
pathids = cycle_or_default(path_ids)
413+
toffsets = cycle_or_default(offsetTrans.transform(offsets), (0, 0))
414+
fcs = cycle_or_default(facecolors)
415+
ecs = cycle_or_default(edgecolors)
416+
lws = cycle_or_default(linewidths)
417+
lss = cycle_or_default(linestyles)
418+
aas = cycle_or_default(antialiaseds)
419+
urls = cycle_or_default(urls)
412420

413421
if Nedgecolors == 0:
414422
gc0.set_linewidth(0.0)
415423

416-
xo, yo = 0, 0
417-
for i in range(N):
418-
path_id = path_ids[i % Npaths]
419-
if Noffsets:
420-
xo, yo = toffsets[i % Noffsets]
424+
for pathid, (xo, yo), fc, ec, lw, ls, aa, url in itertools.islice(
425+
zip(pathids, toffsets, fcs, ecs, lws, lss, aas, urls), N):
421426
if not (np.isfinite(xo) and np.isfinite(yo)):
422427
continue
423-
if Nfacecolors:
424-
rgbFace = facecolors[i % Nfacecolors]
425428
if Nedgecolors:
426429
if Nlinewidths:
427-
gc0.set_linewidth(linewidths[i % Nlinewidths])
430+
gc0.set_linewidth(lw)
428431
if Nlinestyles:
429-
gc0.set_dashes(*linestyles[i % Nlinestyles])
430-
fg = edgecolors[i % Nedgecolors]
431-
if len(fg) == 4:
432-
if fg[3] == 0.0:
433-
gc0.set_linewidth(0)
434-
else:
435-
gc0.set_foreground(fg)
432+
gc0.set_dashes(*ls)
433+
if len(ec) == 4 and ec[3] == 0.0:
434+
gc0.set_linewidth(0)
436435
else:
437-
gc0.set_foreground(fg)
438-
if rgbFace is not None and len(rgbFace) == 4:
439-
if rgbFace[3] == 0:
440-
rgbFace = None
441-
gc0.set_antialiased(antialiaseds[i % Naa])
436+
gc0.set_foreground(ec)
437+
if fc is not None and len(fc) == 4 and fc[3] == 0:
438+
fc = None
439+
gc0.set_antialiased(aa)
442440
if Nurls:
443-
gc0.set_url(urls[i % Nurls])
444-
445-
yield xo, yo, path_id, gc0, rgbFace
441+
gc0.set_url(url)
442+
yield xo, yo, pathid, gc0, fc
446443
gc0.restore()
447444

448445
def get_image_magnification(self):

0 commit comments

Comments
 (0)