12
12
transpose , log , log10 , Float , Float32 , ravel , zeros , Int16 ,\
13
13
Int32 , Int , Float64 , ceil , indices , shape , which , where , sqrt ,\
14
14
asum , resize , reshape , add , argmin , arctan2 , pi , argsort , sin ,\
15
- cos , nonzero , take
15
+ cos , nonzero , take , concatenate , all , newaxis
16
16
17
17
from mlab import linspace , meshgrid
18
18
import _contour
@@ -221,8 +221,8 @@ def break_linecontour(self, linecontour, rot, labelwidth, ind):
221
221
222
222
slc = trans .seq_xy_tups (linecontour )
223
223
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 ()
226
226
227
227
#indices which are under the label
228
228
inds = nonzero (((xx < x + xlabel ) & (xx > x - xlabel )) &
@@ -258,19 +258,30 @@ def break_linecontour(self, linecontour, rot, labelwidth, ind):
258
258
259
259
new_x1d , new_y1d = trans .inverse_xy_tup ((new_x1 , new_y1 ))
260
260
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 ),))
261
263
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 ))
265
264
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))
268
275
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))
271
280
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))
274
285
275
286
return [lc1 ,lc2 ]
276
287
@@ -291,8 +302,8 @@ def locate_label(self, linecontour, labelwidth):
291
302
else :
292
303
ysize = labelwidth
293
304
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 ))
296
307
297
308
yfirst = YY [:,0 ]
298
309
ylast = YY [:,- 1 ]
@@ -305,14 +316,19 @@ def locate_label(self, linecontour, labelwidth):
305
316
L = sqrt ((xlast - xfirst )** 2 + (ylast - yfirst )** 2 )
306
317
dist = add .reduce (([(abs (s )[i ]/ L [i ]) for i in range (xsize )]),- 1 )
307
318
x ,y ,ind = self .get_label_coords (dist , XX , YY , ysize , labelwidth )
319
+ #print 'ind, x, y', ind, x, y
308
320
angle = arctan2 (ylast - yfirst , xlast - xfirst )
309
321
rotation = angle [ind ]* 180 / pi
310
322
if rotation > 90 :
311
323
rotation = rotation - 180
312
324
if rotation < - 90 :
313
325
rotation = 180 + rotation
314
326
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))
316
332
317
333
return x ,y , rotation , dind
318
334
@@ -327,14 +343,15 @@ def labels(self, inline):
327
343
colors ,
328
344
self .label_cvalues , fslist ):
329
345
con = self .collections [icon ]
330
- toremove = []
331
- toadd = []
332
346
lw = self .get_label_width (lev , fmt , fsize )
347
+ additions = []
333
348
for segNum , linecontour in enumerate (con ._segments ):
334
349
# for closed contours add one more point to
335
350
# 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])
338
355
# transfer all data points to screen coordinates
339
356
slc = trans .seq_xy_tups (linecontour )
340
357
if self .print_label (slc ,lw ):
@@ -352,15 +369,9 @@ def labels(self, inline):
352
369
if inline :
353
370
new = self .break_linecontour (linecontour , rotation ,
354
371
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 )
364
375
365
376
class ContourSet (ScalarMappable , ContourLabeler ):
366
377
"""
@@ -443,7 +454,7 @@ def __init__(self, ax, *args, **kwargs):
443
454
lowers = self ._levels [:- 1 ]
444
455
uppers = self ._levels [1 :]
445
456
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 ,
447
458
nchunk = self .nchunk )
448
459
col = PolyCollection (nlist ,
449
460
linewidths = (self .linewidths ,),
@@ -459,7 +470,7 @@ def __init__(self, ax, *args, **kwargs):
459
470
#C = _contour.Cntr(x, y, z.filled(), z.mask())
460
471
C = _contour .Cntr (x , y , z .filled (), ma .getmaskorNone (z ))
461
472
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 )
463
474
col = LineCollection (nlist ,
464
475
colors = color ,
465
476
linewidths = width )
@@ -625,9 +636,20 @@ def _contour_args(self, *args):
625
636
if self .extend in ('both' , 'max' ):
626
637
self ._levels .append (self .zmax + 1 )
627
638
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 ]
628
645
self .layers = self ._levels # contour: a line is a thin layer
629
646
if self .filled :
630
647
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
+
631
653
return (x , y , z )
632
654
633
655
def _process_colors (self ):
@@ -654,13 +676,7 @@ def _process_colors(self):
654
676
else :
655
677
self .cvalues = self .layers
656
678
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 )
664
680
if self .extend in ('both' , 'max' , 'min' ):
665
681
self .norm .clip = False
666
682
self .set_array (self .layers )
0 commit comments