8000 Minor simplification to legend.py. · matplotlib/matplotlib@f930b05 · GitHub
[go: up one dir, main page]

Skip to content

Commit f930b05

Browse files
committed
Minor simplification to legend.py.
Mostly: directly construct the list of (handles, labels) pairs, rather than first construct the two lists separately and then zip them.
1 parent b8364c6 commit f930b05

File tree

1 file changed

+8
-15
lines changed

1 file changed

+8
-15
lines changed

lib/matplotlib/legend.py

Lines changed: 8 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@
2626
unicode_literals)
2727

2828
import six
29-
from six.moves import xrange
3029

3130
import logging
3231
import warnings
@@ -907,15 +906,13 @@ def _init_legend_box(self, handles, labels, markerfirst=True):
907906

908907
text_list = [] # the list of text instances
909908
handle_list = [] # the list of text instances
909+
handles_and_labels = []
910910

911911
label_prop = dict(verticalalignment='baseline',
912912
horizontalalignment='left',
913913
fontproperties=self.prop,
914914
)
915915

916-
labelboxes = []
917-
handleboxes = []
918-
919916
# The approximate height and descent of text. These values are
920917
# only used for plotting the legend handle.
921918
descent = 0.35 * self._approx_text_height() * (self.handleheight - 0.7)
@@ -948,26 +945,23 @@ def _init_legend_box(self, handles, labels, markerfirst=True):
948945
textbox = TextArea(lab, textprops=label_prop,
949946
multilinebaseline=True,
950947
minimumdescent=True)
951-
text_list.append(textbox._text)
952-
953-
labelboxes.append(textbox)
954-
955948
handlebox = DrawingArea(width=self.handlelength * fontsize,
956949
height=height,
957950
xdescent=0., ydescent=descent)
958-
handleboxes.append(handlebox)
959951

952+
text_list.append(textbox._text)
960953
# Create the artist for the legend which represents the
961954
# original artist/handle.
962955
handle_list.append(handler.legend_artist(self, orig_handle,
963956
fontsize, handlebox))
957+
handles_and_labels.append((handlebox, textbox))
964958

965-
if handleboxes:
959+
if handles_and_labels:
966960
# We calculate number of rows in each column. The first
967961
# (num_largecol) columns will have (nrows+1) rows, and remaining
968962
# (num_smallcol) columns will have (nrows) rows.
969-
ncol = min(self._ncol, len(handleboxes))
970-
nrows, num_largecol = divmod(len(handleboxes), ncol)
963+
ncol = min(self._ncol, len(handles_and_labels))
964+
nrows, num_largecol = divmod(len(handles_and_labels), ncol)
971965
num_smallcol = ncol - num_largecol
972966
# starting index of each column and number of rows in it.
973967
rows_per_col = [nrows + 1] * num_largecol + [nrows] * num_smallcol
@@ -976,15 +970,14 @@ def _init_legend_box(self, handles, labels, markerfirst=True):
976970
else:
977971
cols = []
978972

979-
handle_label = list(zip(handleboxes, labelboxes))
980973
columnbox = []
981974
for i0, di in cols:
982975
# pack handleBox and labelBox into itemBox
983976
itemBoxes = [HPacker(pad=0,
984977
sep=self.handletextpad * fontsize,
985978
children=[h, t] if markerfirst else [t, h],
986979
align="baseline")
987-
for h, t in handle_label[i0:i0 + di]]
980+
for h, t in handles_and_labels[i0:i0 + di]]
988981
# minimumdescent=False for the text of the last row of the column
989982
if markerfirst:
990983
itemBoxes[-1].get_children()[1].set_minimumdescent(False)
@@ -1207,7 +1200,7 @@ def _get_anchored_bbox(self, loc, bbox, parentbbox, renderer):
12071200
"""
12081201
assert loc in range(1, 11) # called only internally
12091202

1210-
BEST, UR, UL, LL, LR, R, CL, CR, LC, UC, C = list(xrange(11))
1203+
BEST, UR, UL, LL, LR, R, CL, CR, LC, UC, C = range(11)
12111204

12121205
anchor_coefs = {UR: "NE",
12131206
UL: "NW",

0 commit comments

Comments
 (0)
0