Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 0026439

Browse files
committed
print out more meaningful messages when legend is called with artists that are not supported
svn path=/trunk/matplotlib/; revision=8014
1 parent a34d29a commit 0026439

2 files changed

Lines changed: 42 additions & 31 deletions

File tree

lib/matplotlib/legend.py

Lines changed: 39 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -432,14 +432,6 @@ def _init_legend_box(self, handles, labels):
432432
)
433433

434434
labelboxes = []
435-
436-
for l in labels:
437-
textbox = TextArea(l, textprops=label_prop,
438-
multilinebaseline=True, minimumdescent=True)
439-
text_list.append(textbox._text)
440-
441-
labelboxes.append(textbox)
442-
443435
handleboxes = []
444436

445437

@@ -457,7 +449,8 @@ def _init_legend_box(self, handles, labels):
457449
# default trasnform (eg, Collections), you need to
458450
# manually set their transform to the self.get_transform().
459451

460-
for handle in handles:
452+
453+
for handle, lab in zip(handles, labels):
461454
if isinstance(handle, RegularPolyCollection) or \
462455
isinstance(handle, CircleCollection):
463456
npoints = self.scatterpoints
@@ -578,32 +571,47 @@ def _init_legend_box(self, handles, labels):
578571
p.set_clip_path(None)
579572
handle_list.append(p)
580573
else:
574+
handle_type = type(handle)
575+
warnings.warn("Legend does not support %s\nUse proxy artist instead.\n\nhttp://matplotlib.sourceforge.net/users/legend_guide.html#using-proxy-artist\n" % (str(handle_type),))
581576
handle_list.append(None)
582577

583-
handlebox = DrawingArea(width=self.handlelength*fontsize,
584-
height=height,
585-
xdescent=0., ydescent=descent)
586-
587-
handle = handle_list[-1]
588-
handlebox.add_artist(handle)
589-
if hasattr(handle, "_legmarker"):
590-
handlebox.add_artist(handle._legmarker)
591-
handleboxes.append(handlebox)
592578

593579

594-
# We calculate number of lows in each column. The first
595-
# (num_largecol) columns will have (nrows+1) rows, and remaing
596-
# (num_smallcol) columns will have (nrows) rows.
597-
ncol = min(self._ncol, len(handleboxes))
598-
nrows, num_largecol = divmod(len(handleboxes), ncol)
599-
num_smallcol = ncol-num_largecol
600-
601-
# starting index of each column and number of rows in it.
602-
largecol = safezip(range(0, num_largecol*(nrows+1), (nrows+1)),
603-
[nrows+1] * num_largecol)
604-
smallcol = safezip(range(num_largecol*(nrows+1), len(handleboxes), nrows),
605-
[nrows] * num_smallcol)
606-
580+
handle = handle_list[-1]
581+
if handle is not None: # handle is None is the artist is not supproted
582+
textbox = TextArea(lab, textprops=label_prop,
583+
multilinebaseline=True, minimumdescent=True)
584+
text_list.append(textbox._text)
585+
586+
labelboxes.append(textbox)
587+
588+
handlebox = DrawingArea(width=self.handlelength*fontsize,
589+
height=height,
590+
xdescent=0., ydescent=descent)
591+
592+
handlebox.add_artist(handle)
593+
if hasattr(handle, "_legmarker"):
594+
handlebox.add_artist(handle._legmarker)
595+
handleboxes.append(handlebox)
596+
597+
598+
if len(handleboxes) > 0:
599+
600+
# We calculate number of lows in each column. The first
601+
# (num_largecol) columns will have (nrows+1) rows, and remaing
602+
# (num_smallcol) columns will have (nrows) rows.
603+
ncol = min(self._ncol, len(handleboxes))
604+
nrows, num_largecol = divmod(len(handleboxes), ncol)
605+
num_smallcol = ncol-num_largecol
606+
607+
# starting index of each column and number of rows in it.
608+
largecol = safezip(range(0, num_largecol*(nrows+1), (nrows+1)),
609+
[nrows+1] * num_largecol)
610+
smallcol = safezip(range(num_largecol*(nrows+1), len(handleboxes), nrows),
611+
[nrows] * num_smallcol)
612+
else:
613+
largecol, smallcol = [], []
614+
607615
handle_label = safezip(handleboxes, labelboxes)
608616
columnbox = []
609617
for i0, di in largecol+smallcol:

lib/matplotlib/offsetbox.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -375,6 +375,9 @@ def get_extent_offsets(self, renderer):
375375

376376
whd_list = [c.get_extent(renderer) for c in self.get_visible_children()]
377377

378+
if not whd_list:
379+
return 2*pad, 2*pad, pad, pad, []
380+
378381
if self.height is None:
379382
height_descent = max([h-yd for w,h,xd,yd in whd_list])
380383
ydescent = max([yd for w,h,xd,yd in whd_list])

0 commit comments

Comments
 (0)