Skip to content

Commit 35c4b07

Browse files
committed
Use cycling iterators in RendererBase.
This defines all the cycling behavior in a single block, rather than spreading modulo operations all over.
1 parent 8ddeea1 commit 35c4b07

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 re
@@ -394,61 +395,57 @@ def _iter_collection(self, gc, master_transform, all_transforms,
394395
*path_ids*; *gc* is a graphics context and *rgbFace* is a color to
395396
use for filling the path.
396397
"""
397-
Ntransforms = len(all_transforms)
398398
Npaths = len(path_ids)
399399
Noffsets = len(offsets)
400400
N = max(Npaths, Noffsets)
401401
Nfacecolors = len(facecolors)
402402
Nedgecolors = len(edgecolors)
403403
Nlinewidths = len(linewidths)
404404
Nlinestyles = len(linestyles)
405-
Naa = len(antialiaseds)
406405
Nurls = len(urls)
407406

408407
if (Nfacecolors == 0 and Nedgecolors == 0) or Npaths == 0:
409408
return
410-
if Noffsets:
411-
toffsets = offsetTrans.transform(offsets)
412409

413410
gc0 = self.new_gc()
414411
gc0.copy_properties(gc)
415412

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

419427
if Nedgecolors == 0:
420428
gc0.set_linewidth(0.0)
421429

422-
xo, yo = 0, 0
423-
for i in range(N):
424-
path_id = path_ids[i % Npaths]
425-
if Noffsets:
426-
xo, yo = toffsets[i % Noffsets]
430+
for pathid, (xo, yo), fc, ec, lw, ls, aa, url in itertools.islice(
431+
zip(pathids, toffsets, fcs, ecs, lws, lss, aas, urls), N):
427432
if not (np.isfinite(xo) and np.isfinite(yo)):
428433
continue
429-
if Nfacecolors:
430-
rgbFace = facecolors[i % Nfacecolors]
431434
if Nedgecolors:
432435
if Nlinewidths:
433-
gc0.set_linewidth(linewidths[i % Nlinewidths])
436+
gc0.set_linewidth(lw)
434437
if Nlinestyles:
435-
gc0.set_dashes(*linestyles[i % Nlinestyles])
436-
fg = edgecolors[i % Nedgecolors]
437-
if len(fg) == 4:
438-
if fg[3] == 0.0:
439-
gc0.set_linewidth(0)
440-
else:
441-
gc0.set_foreground(fg)
438+
gc0.set_dashes(*ls)
439+
if len(ec) == 4 and ec[3] == 0.0:
440+
gc0.set_linewidth(0)
442441
else:
443-
gc0.set_foreground(fg)
444-
if rgbFace is not None and len(rgbFace) == 4:
445-
if rgbFace[3] == 0:
446-
rgbFace = None
447-
gc0.set_antialiased(antialiaseds[i % Naa])
442+
gc0.set_foreground(ec)
443+
if fc is not None and len(fc) == 4 and fc[3] == 0:
444+
fc = None
445+
gc0.set_antialiased(aa)
448446
if Nurls:
449-
gc0.set_url(urls[i % Nurls])
450-
451-
yield xo, yo, path_id, gc0, rgbFace
447+
gc0.set_url(url)
448+
yield xo, yo, pathid, gc0, fc
452449
gc0.restore()
453450

454451
def get_image_magnification(self):

0 commit comments

Comments
 (0)