Skip to content

Commit 338f6da

Browse files
committedDec 21, 2016
safezip is (mostly) overrated.
1 parent 39352a8 commit 338f6da

File tree

2 files changed

+16
-32
lines changed

2 files changed

+16
-32
lines changed
 

‎lib/matplotlib/axes/_axes.py

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2571,11 +2571,11 @@ def pie(self, x, explode=None, labels=None, colors=None,
25712571
labels.
25722572
"""
25732573

2574-
x = np.asarray(x).astype(np.float32)
2574+
x = np.array(x, np.float32)
25752575

2576-
sx = float(x.sum())
2576+
sx = x.sum()
25772577
if sx > 1:
2578-
x = np.divide(x, sx)
2578+
x /= sx
25792579

25802580
if labels is None:
25812581
labels = [''] * len(x)
@@ -2605,20 +2605,18 @@ def get_next_color():
26052605
# set default values in wedge_prop
26062606
if wedgeprops is None:
26072607
wedgeprops = {}
2608-
if 'clip_on' not in wedgeprops:
2609-
wedgeprops['clip_on'] = False
2608+
wedgeprops.setdefault('clip_on', False)
26102609

26112610
if textprops is None:
26122611
textprops = {}
2613-
if 'clip_on' not in textprops:
2614-
textprops['clip_on'] = False
2612+
textprops.setdefault('clip_on', False)
26152613

26162614
texts = []
26172615
slices = []
26182616
autotexts = []
26192617

26202618
i = 0
2621-
for frac, label, expl in cbook.safezip(x, labels, explode):
2619+
for frac, label, expl in zip(x, labels, explode):
26222620
x, y = center
26232621
theta2 = (theta1 + frac) if counterclock else (theta1 - frac)
26242622
thetam = 2 * math.pi * 0.5 * (theta1 + theta2)

‎lib/matplotlib/legend.py

Lines changed: 10 additions & 24 deletions
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)
Please sign in to comment.