Skip to content

Commit 017ca24

Browse files
committed
safezip is (mostly) overrated.
1 parent 2652633 commit 017ca24

File tree

2 files changed

+16
-32
lines changed

2 files changed

+16
-32
lines changed

lib/matplotlib/axes/_axes.py

+6-8
Original file line numberDiff line numberDiff line change
@@ -2576,11 +2576,11 @@ def pie(self, x, explode=None, labels=None, colors=None,
25762576
labels.
25772577
"""
25782578

2579-
x = np.asarray(x).astype(np.float32)
2579+
x = np.array(x, np.float32)
25802580

2581-
sx = float(x.sum())
2581+
sx = x.sum()
25822582
if sx > 1:
2583-
x = np.divide(x, sx)
2583+
x /= sx
25842584

25852585
if labels is None:
25862586
labels = [''] * len(x)
@@ -2610,20 +2610,18 @@ def get_next_color():
26102610
# set default values in wedge_prop
26112611
if wedgeprops is None:
26122612
wedgeprops = {}
2613-
if 'clip_on' not in wedgeprops:
2614-
wedgeprops['clip_on'] = False
2613+
wedgeprops.setdefault('clip_on', False)
26152614

26162615
if textprops is None:
26172616
textprops = {}
2618-
if 'clip_on' not in textprops:
2619-
textprops['clip_on'] = False
2617+
textprops.setdefault('clip_on', False)
26202618

26212619
texts = []
26222620
slices = []
26232621
autotexts = []
26242622

26252623
i = 0
2626-
for frac, label, expl in cbook.safezip(x, labels, explode):
2624+
for frac, label, expl in zip(x, labels, explode):
26272625
x, y = center
26282626
theta2 = (theta1 + frac) if counterclock else (theta1 - frac)
26292627
thetam = 2 * math.pi * 0.5 * (theta1 + theta2)

lib/matplotlib/legend.py

+10-24
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,7 @@
3333

3434
from matplotlib import rcParams
3535
from matplotlib.artist import Artist, allow_rasterization
36-
from matplotlib.cbook import (is_string_like, iterable, silent_list, safezip,
37-
is_hashable)
36+
from matplotlib.cbook import is_string_like, iterable, silent_list, is_hashable
3837
from matplotlib.font_manager import FontProperties
3938
from matplotlib.lines import Line2D
4039
from matplotlib.patches import Patch, Rectangle, Shadow, FancyBboxPatch
@@ -653,29 +652,23 @@ def _init_legend_box(self, handles, labels, markerfirst=True):
653652
handle_list.append(handler.legend_artist(self, orig_handle,
654653
fontsize, handlebox))
655654

656-
if len(handleboxes) > 0:
657-
655+
if handleboxes:
658656
# We calculate number of rows in each column. The first
659657
# (num_largecol) columns will have (nrows+1) rows, and remaining
660658
# (num_smallcol) columns will have (nrows) rows.
661659
ncol = min(self._ncol, len(handleboxes))
662660
nrows, num_largecol = divmod(len(handleboxes), ncol)
663661
num_smallcol = ncol - num_largecol
664-
665662
# starting index of each column and number of rows in it.
666-
largecol = safezip(list(xrange(0,
667-
num_largecol * (nrows + 1),
668-
(nrows + 1))),
669-
[nrows + 1] * num_largecol)
670-
smallcol = safezip(list(xrange(num_largecol * (nrows + 1),
671-
len(handleboxes), nrows)),
672-
[nrows] * num_smallcol)
663+
rows_per_col = [nrows + 1] * num_largecol + [nrows] * num_smallcol
664+
start_idxs = np.concatenate([[0], np.cumsum(rows_per_col)[:-1]])
665+
cols = zip(start_idxs, rows_per_col)
673666
else:
674-
largecol, smallcol = [], []
667+
cols = []
675668

676-
handle_label = safezip(handleboxes, labelboxes)
669+
handle_label = list(zip(handleboxes, labelboxes))
677670
columnbox = []
678-
for i0, di in largecol + smallcol:
671+
for i0, di in cols:
679672
# pack handleBox and labelBox into itemBox
680673
itemBoxes = [HPacker(pad=0,
681674
sep=self.handletextpad * fontsize,
@@ -689,20 +682,13 @@ def _init_legend_box(self, handles, labels, markerfirst=True):
689682
itemBoxes[-1].get_children()[0].set_minimumdescent(False)
690683

691684
# pack columnBox
692-
if markerfirst:
693-
alignment = "baseline"
694-
else:
695-
alignment = "right"
685+
alignment = "baseline" if markerfirst else "right"
696686
columnbox.append(VPacker(pad=0,
697687
sep=self.labelspacing * fontsize,
698688
align=alignment,
699689
children=itemBoxes))
700690

701-
if self._mode == "expand":
702-
mode = "expand"
703-
else:
704-
mode = "fixed"
705-
691+
mode = "expand" if self._mode == "expand" else "fixed"
706692
sep = self.columnspacing * fontsize
707693
self._legend_handle_box = HPacker(pad=0,
708694
sep=sep, align="baseline",

0 commit comments

Comments
 (0)