1212 transpose , log , log10 , Float , Float32 , ravel , zeros , Int16 ,\
1313 Int32 , Int , Float64 , ceil , indices , shape , which , where , sqrt ,\
1414 asum , resize , reshape , add , argmin , arctan2 , pi , argsort , sin ,\
15- cos , nonzero , take
15+ cos , nonzero , take , concatenate , all , newaxis
1616
1717from mlab import linspace , meshgrid
1818import _contour
@@ -221,8 +221,8 @@ def break_linecontour(self, linecontour, rot, labelwidth, ind):
221221
222222 slc = trans .seq_xy_tups (linecontour )
223223 x ,y = slc [ind ]
224- xx = array (slc )[:,0 ].copy ()
225- yy = array (slc )[:,1 ].copy ()
224+ xx = asarray (slc )[:,0 ].copy ()
225+ yy = asarray (slc )[:,1 ].copy ()
226226
227227 #indices which are under the label
228228 inds = nonzero (((xx < x + xlabel ) & (xx > x - xlabel )) &
@@ -258,19 +258,30 @@ def break_linecontour(self, linecontour, rot, labelwidth, ind):
258258
259259 new_x1d , new_y1d = trans .inverse_xy_tup ((new_x1 , new_y1 ))
260260 new_x2d , new_y2d = trans .inverse_xy_tup ((new_x2 , new_y2 ))
261+ new_xy1 = array (((new_x1d , new_y1d ),))
262+ new_xy2 = array (((new_x2d , new_y2d ),))
261263
262- if rot > 0 :
263- if len (lc1 ) > 0 and (lc1 [- 1 ][0 ] <= new_x1d ) and (lc1 [- 1 ][1 ] <= new_y1d ):
264- lc1 .append ((new_x1d , new_y1d ))
265264
266- if len (lc2 ) > 0 and (lc2 [0 ][0 ] >= new_x2d ) and (lc2 [0 ][1 ] >= new_y2d ):
267- lc2 .insert (0 , (new_x2d , new_y2d ))
265+ if rot > 0 :
266+ if (len (lc1 ) > 0 and (lc1 [- 1 ][0 ] <= new_x1d )
267+ and (lc1 [- 1 ][1 ] <= new_y1d )):
268+ lc1 = concatenate ((lc1 , new_xy1 ))
269+ #lc1.append((new_x1d, new_y1d))
270+
271+ if (len (lc2 ) > 0 and (lc2 [0 ][0 ] >= new_x2d )
272+ and (lc2 [0 ][1 ] >= new_y2d )):
273+ lc2 = concatenate ((new_xy2 , lc2 ))
274+ #lc2.insert(0, (new_x2d, new_y2d))
268275 else :
269- if len (lc1 ) > 0 and ((lc1 [- 1 ][0 ] <= new_x1d ) and (lc1 [- 1 ][1 ] >= new_y1d )):
270- lc1 .append ((new_x1d , new_y1d ))
276+ if (len (lc1 ) > 0 and ((lc1 [- 1 ][0 ] <= new_x1d )
277+ and (lc1 [- 1 ][1 ] >= new_y1d ))):
278+ lc1 = concatenate ((lc1 , new_xy1 ))
279+ #lc1.append((new_x1d, new_y1d))
271280
272- if len (lc2 ) > 0 and ((lc2 [0 ][0 ] >= new_x2d ) and (lc2 [0 ][1 ] <= new_y2d )):
273- lc2 .insert (0 , (new_x2d , new_y2d ))
281+ if (len (lc2 ) > 0 and ((lc2 [0 ][0 ] >= new_x2d )
282+ and (lc2 [0 ][1 ] <= new_y2d ))):
283+ lc2 = concatenate ((new_xy2 , lc2 ))
284+ #lc2.insert(0, (new_x2d, new_y2d))
274285
275286 return [lc1 ,lc2 ]
276287
@@ -291,8 +302,8 @@ def locate_label(self, linecontour, labelwidth):
291302 else :
292303 ysize = labelwidth
293304
294- XX = resize (array (linecontour )[:,0 ],(xsize , ysize ))
295- YY = resize (array (linecontour )[:,1 ],(xsize ,ysize ))
305+ XX = resize (asarray (linecontour )[:,0 ],(xsize , ysize ))
306+ YY = resize (asarray (linecontour )[:,1 ],(xsize ,ysize ))
296307
297308 yfirst = YY [:,0 ]
298309 ylast = YY [:,- 1 ]
@@ -305,14 +316,19 @@ def locate_label(self, linecontour, labelwidth):
305316 L = sqrt ((xlast - xfirst )** 2 + (ylast - yfirst )** 2 )
306317 dist = add .reduce (([(abs (s )[i ]/ L [i ]) for i in range (xsize )]),- 1 )
307318 x ,y ,ind = self .get_label_coords (dist , XX , YY , ysize , labelwidth )
319+ #print 'ind, x, y', ind, x, y
308320 angle = arctan2 (ylast - yfirst , xlast - xfirst )
309321 rotation = angle [ind ]* 180 / pi
310322 if rotation > 90 :
311323 rotation = rotation - 180
312324 if rotation < - 90 :
313325 rotation = 180 + rotation
314326
315- dind = list (linecontour ).index ((x ,y ))
327+ # There must be a more efficient way...
328+ lc = [tuple (l ) for l in linecontour ]
329+ dind = lc .index ((x ,y ))
330+ #print 'dind', dind
331+ #dind = list(linecontour).index((x,y))
316332
317333 return x ,y , rotation , dind
318334
@@ -327,14 +343,15 @@ def labels(self, inline):
327343 colors ,
328344 self .label_cvalues , fslist ):
329345 con = self .collections [icon ]
330- toremove = []
331- toadd = []
332346 lw = self .get_label_width (lev , fmt , fsize )
347+ additions = []
333348 for segNum , linecontour in enumerate (con ._segments ):
334349 # for closed contours add one more point to
335350 # avoid division by zero
336- if linecontour [0 ] == linecontour [- 1 ]:
337- linecontour .append (linecontour [1 ])
351+ if all (linecontour [0 ] == linecontour [- 1 ]):
352+ linecontour = concatenate ((linecontour ,
353+ linecontour [1 ][newaxis ,:]))
354+ #linecontour.append(linecontour[1])
338355 # transfer all data points to screen coordinates
339356 slc = trans .seq_xy_tups (linecontour )
340357 if self .print_label (slc ,lw ):
@@ -352,15 +369,9 @@ def labels(self, inline):
352369 if inline :
353370 new = self .break_linecontour (linecontour , rotation ,
354371 lw , ind )
355- toadd .extend (new )
356- #for c in new: toadd.append(c)
357- toremove .append (linecontour )
358- for c in toremove :
359- con ._segments .remove (c )
360- for c in toadd :
361- con ._segments .append (c )
362-
363-
372+ con ._segments [segNum ] = new [0 ]
373+ additions .append (new [1 ])
374+ con ._segments .extend (additions )
364375
365376class ContourSet (ScalarMappable , ContourLabeler ):
366377 """
@@ -443,7 +454,7 @@ def __init__(self, ax, *args, **kwargs):
443454 lowers = self ._levels [:- 1 ]
444455 uppers = self ._levels [1 :]
445456 for level , level_upper , color in zip (lowers , uppers , self .tcolors ):
446- nlist = C .trace (level , level_upper , points = 1 ,
457+ nlist = C .trace (level , level_upper , points = 0 ,
447458 nchunk = self .nchunk )
448459 col = PolyCollection (nlist ,
449460 linewidths = (self .linewidths ,),
@@ -459,7 +470,7 @@ def __init__(self, ax, *args, **kwargs):
459470 #C = _contour.Cntr(x, y, z.filled(), z.mask())
460471 C = _contour .Cntr (x , y , z .filled (), ma .getmaskorNone (z ))
461472 for level , color , width in zip (self .levels , self .tcolors , tlinewidths ):
462- nlist = C .trace (level , points = 1 )
473+ nlist = C .trace (level , points = 0 )
463474 col = LineCollection (nlist ,
464475 colors = color ,
465476 linewidths = width )
@@ -625,9 +636,20 @@ def _contour_args(self, *args):
625636 if self .extend in ('both' , 'max' ):
626637 self ._levels .append (self .zmax + 1 )
627638 self ._levels = asarray (self ._levels )
639+ self .vmin = amin (self .levels ) # alternative would be self.layers
640+ self .vmax = amax (self .levels )
641+ if self .extend in ('both' , 'min' ) or self .clip_ends :
642+ self .vmin = 2 * self .levels [0 ] - self .levels [1 ]
643+ if self .extend in ('both' , 'max' ) or self .clip_ends :
644+ self .vmax = 2 * self .levels [- 1 ] - self .levels [- 2 ]
628645 self .layers = self ._levels # contour: a line is a thin layer
629646 if self .filled :
630647 self .layers = 0.5 * (self ._levels [:- 1 ] + self ._levels [1 :])
648+ if self .extend in ('both' , 'min' ) or self .clip_ends :
649+ self .layers [0 ] = 0.5 * (self .vmin + self ._levels [1 ])
650+ if self .extend in ('both' , 'max' ) or self .clip_ends :
651+ self .layers [- 1 ] = 0.5 * (self .vmax + self ._levels [- 2 ])
652+
631653 return (x , y , z )
632654
633655 def _process_colors (self ):
@@ -654,13 +676,7 @@ def _process_colors(self):
654676 else :
655677 self .cvalues = self .layers
656678 if not self .norm .scaled ():
657- vmin = amin (self .levels ) # alternative would be self.layers
658- vmax = amax (self .levels )
659- if self .extend in ('both' , 'min' ) or self .clip_ends :
660- vmin = 2 * self .levels [0 ] - self .levels [1 ]
661- if self .extend in ('both' , 'max' ) or self .clip_ends :
662- vmax = 2 * self .levels [- 1 ] - self .levels [- 2 ]
663- self .set_clim (vmin , vmax )
679+ self .set_clim (self .vmin , self .vmax )
664680 if self .extend in ('both' , 'max' , 'min' ):
665681 self .norm .clip = False
666682 self .set_array (self .layers )
0 commit comments