Skip to content

Commit 6388198

Browse files
committed
fixed labelsep legend bug
svn path=/trunk/matplotlib/; revision=1084
1 parent 4cea7a5 commit 6388198

File tree

4 files changed

+34
-12
lines changed

4 files changed

+34
-12
lines changed

CHANGELOG

+1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
New entries should be added at the top
2+
2005-03-16 Fixed labelsep bug
23

34
2005-03-16 Applied Darren's ticker fix for small ranges - JDH
45

TODO

+3-1
Original file line numberDiff line numberDiff line change
@@ -669,4 +669,6 @@ ZeroDivisionError: SeparableTransformation::eval_scalars yin interval is zero; c
669669
contours is that it doesn't carry over when the list is sliced, eg
670670
when passing every 2nd contour and level to the labeler.
671671

672-
-- colorbar image origin with contour is broken in SVG; test PS
672+
-- colorbar image origin with contour is broken in SVG; test PS
673+
674+
-- Bryan Cole's CXX type segfault

lib/matplotlib/contour.py

+26-11
Original file line numberDiff line numberDiff line change
@@ -221,7 +221,7 @@ def too_close(self, x,y, lw):
221221
if self.cl_xy != []:
222222
dist = [sqrt((x-loc[0]) ** 2 + (y-loc[1]) ** 2) for loc in self.cl_xy]
223223
for d in dist:
224-
if d < 150:
224+
if d < 1.2*lw:
225225
return 1
226226
else: return 0
227227
else: return 0
@@ -292,16 +292,29 @@ def break_linecontour(self, linecontour, rot, labelwidth, ind):
292292
xx= array(slc)[:,0].copy()
293293
yy=array(slc)[:,1].copy()
294294

295+
#indices which are under the label
295296
inds=nonzero(((xx < x+xlabel) & (xx > x-xlabel)) & ((yy < y+ylabel) & (yy > y-ylabel)))
296297

297298
if len(inds) >0:
298-
lc1=linecontour[:inds[0]]
299-
lc2 = linecontour[inds[-1]+1:]
299+
#if the label happens to be over the beginning of the
300+
#contour, the entire contour is removed, i.e.
301+
#indices to be removed are
302+
#inds= [0,1,2,3,305,306,307]
303+
#should rewrite this in a better way
304+
linds = nonzero(inds[1:]- inds[:-1] != 1)
305+
if inds[0] == 0 and len(linds) != 0:
306+
ii = inds[linds[0]]
307+
lc1 =linecontour[ii+1:inds[ii+1]]
308+
lc2 = []
309+
310+
else:
311+
lc1=linecontour[:inds[0]]
312+
lc2= linecontour[inds[-1]+1:]
313+
300314
else:
301315
lc1=linecontour[:ind]
302316
lc2 = linecontour[ind+1:]
303317

304-
epsilon=.000005
305318

306319
if rot <0:
307320
new_x1, new_y1 = x-xlabel, y+ylabel
@@ -371,7 +384,6 @@ def inline_labels(self, levels, contours, colors, fslist, fmt):
371384
trans = self.ax.transData
372385
contourNum = 0
373386
for lev, con, color, fsize in zip(levels, contours, colors, fslist):
374-
col = []
375387
toremove = []
376388
toadd = []
377389
lw = self.get_label_width(lev, fmt, fsize)
@@ -394,7 +406,6 @@ def inline_labels(self, levels, contours, colors, fslist, fmt):
394406
self.set_label_props(t, text, color)
395407
self.cl.append(t)
396408
new = self.break_linecontour(linecontour, rotation, lw, ind)
397-
for c in new: col.append(c)
398409

399410
for c in new: toadd.append(c)
400411
toremove.append(linecontour)
@@ -539,8 +550,10 @@ def _contour_args(self, filled, badmask, origin, extent, *args):
539550
lev = array([float(fl) for fl in level_arg])
540551
else:
541552
raise TypeError("Last %s arg must give levels; see help(%s)" % (fn,fn))
542-
self.ax.set_xlim((min(ravel(x)), max(ravel(x))))
543-
self.ax.set_ylim((min(ravel(y)), max(ravel(y))))
553+
rx = ravel(x)
554+
ry = ravel(y)
555+
self.ax.set_xlim((min(rx), max(rx)))
556+
self.ax.set_ylim((min(ry), max(ry)))
544557
return (x, y, z, lev)
545558

546559

@@ -707,6 +720,7 @@ def contour(self, *args, **kwargs):
707720
if extent is not None: assert(len(extent) == 4)
708721
if colors is not None and cmap is not None:
709722
raise RuntimeError('Either colors or cmap must be None')
723+
# todo: shouldn't this use the current image rather than the rc param?
710724
if origin == 'image': origin = rcParams['image.origin']
711725

712726

@@ -727,9 +741,8 @@ def contour(self, *args, **kwargs):
727741

728742
reg, triangle = self._initialize_reg_tri(z, badmask)
729743

730-
tcolors, mappable, collections = self._process_colors(z, colors,
731-
alpha,
732-
lev, cmap)
744+
tcolors, mappable, collections = self._process_colors(
745+
z, colors, alpha, lev, cmap)
733746

734747
if linewidths == None:
735748
tlinewidths = [rcParams['lines.linewidth']] *Nlev
@@ -760,6 +773,8 @@ def contour(self, *args, **kwargs):
760773
collections.append(col)
761774

762775
collections = silent_list('LineCollection', collections)
776+
# the mappable attr is for the pylab interface functions,
777+
# which maintain the current image
763778
collections.mappable = mappable
764779
return lev, collections
765780

lib/matplotlib/legend.py

+4
Original file line numberDiff line numberDiff line change
@@ -327,11 +327,15 @@ def get_tbounds(text): #get text bounds in axes coords
327327
for t, tabove in zip(self.texts[1:], self.texts[:-1]):
328328
x,y = t.get_position()
329329
l,b,w,h = get_tbounds(tabove)
330+
b -= self.labelsep
331+
h += 2*self.labelsep
330332
hpos.append( (b,h) )
331333
t.set_position( (x, b-0.1*h) )
332334

333335
# now do the same for last line
334336
l,b,w,h = get_tbounds(self.texts[-1])
337+
b -= self.labelsep
338+
h += 2*self.labelsep
335339
hpos.append( (b,h) )
336340

337341
for handle, tup in zip(self.handles, hpos):

0 commit comments

Comments
 (0)