@@ -272,7 +272,7 @@ class Slider(AxesWidget):
272
272
"""
273
273
def __init__ (self , ax , label , valmin , valmax , valinit = 0.5 , valfmt = '%1.2f' ,
274
274
closedmin = True , closedmax = True , slidermin = None ,
275
- slidermax = None , dragging = True , valstep = None , ** kwargs ):
275
+ slidermax = None , dragging = True , valstep = None , orientation = 'h' , ** kwargs ):
276
276
"""
277
277
Parameters
278
278
----------
@@ -314,6 +314,9 @@ def __init__(self, ax, label, valmin, valmax, valinit=0.5, valfmt='%1.2f',
314
314
valstep : float, optional, default: None
315
315
If given, the slider will snap to multiples of `valstep`.
316
316
317
+ orientation : str, optional, default: 'h'
318
+ If 'h' the slider is horizontal. If 'v' it is vertical
319
+
317
320
Notes
318
321
-----
319
322
Additional kwargs are passed on to ``self.poly`` which is the
@@ -329,6 +332,11 @@ def __init__(self, ax, label, valmin, valmax, valinit=0.5, valfmt='%1.2f',
329
332
if slidermax is not None and not hasattr (slidermax , 'val' ):
330
333
raise ValueError ("Argument slidermax ({}) has no 'val'"
331
334
.format (type (slidermax )))
335
+ if orientation not in ['h' , 'v' ]:
336
+ raise ValueError ("Argument orientation ({}) must be either 'h' or 'v'"
337
+ .format (orientation ))
338
+
339
+ self .orientation = orientation
332
340
self .closedmin = closedmin
333
341
self .closedmax = closedmax
334
342
self .slidermin = slidermin
@@ -342,27 +350,44 @@ def __init__(self, ax, label, valmin, valmax, valinit=0.5, valfmt='%1.2f',
342
350
valinit = valmin
343
351
self .val = valinit
344
352
self .valinit = valinit
345
- self .poly = ax .axvspan (valmin , valinit , 0 , 1 , ** kwargs )
346
- self .vline = ax .axvline (valinit , 0 , 1 , color = 'r' , lw = 1 )
353
+ if orientation is 'v' :
354
+ self .poly = ax .axhspan (valmin , valinit , 0 , 1 , ** kwargs )
355
+ self .hline = ax .axhline (valinit , 0 , 1 , color = 'r' , lw = 1 )
356
+ else :
357
+ self .poly = ax .axvspan (valmin , valinit , 0 , 1 , ** kwargs )
358
+ self .vline = ax .axvline (valinit , 0 , 1 , color = 'r' , lw = 1 )
347
359
348
360
self .valfmt = valfmt
349
361
ax .set_yticks ([])
350
- ax .set_xlim ((valmin , valmax ))
362
+ if orientation is 'v' :
363
+ ax .set_ylim ((valmin , valmax ))
364
+ else :
365
+ ax .set_xlim ((valmin , valmax ))
351
366
ax .set_xticks ([])
352
367
ax .set_navigate (False )
353
368
354
369
self .connect_event ('button_press_event' , self ._update )
355
370
self .connect_event ('button_release_event' , self ._update )
356
371
if dragging :
357
372
self .connect_event ('motion_notify_event' , self ._update )
358
- self .label = ax .text (- 0.02 , 0.5 , label , transform = ax .transAxes ,
359
- verticalalignment = 'center' ,
360
- horizontalalignment = 'right' )
373
+ if orientation is 'v' :
374
+ self .label = ax .text (0.5 , 1.02 , label , transform = ax .transAxes ,
375
+ verticalalignment = 'bottom' ,
376
+ horizontalalignment = 'center' )
377
+
378
+ self .valtext = ax .text (0.5 , - 0.02 , valfmt % valinit ,
379
+ transform = ax .transAxes ,
380
+ verticalalignment = 'top' ,
381
+ horizontalalignment = 'center' )
382
+ else :
383
+ self .label = ax .text (- 0.02 , 0.5 , label , transform = ax .transAxes ,
384
+ verticalalignment = 'center' ,
385
+ horizontalalignment = 'right' )
361
386
362
- self .valtext = ax .text (1.02 , 0.5 , valfmt % valinit ,
363
- transform = ax .transAxes ,
364
- verticalalignment = 'center' ,
365
- horizontalalignment = 'left' )
387
+ self .valtext = ax .text (1.02 , 0.5 , valfmt % valinit ,
388
+ transform = ax .transAxes ,
389
+ verticalalignment = 'center' ,
390
+ horizontalalignment = 'left' )
366
391
367
392
self .cnt = 0
368
393
self .observers = {}
@@ -416,7 +441,10 @@ def _update(self, event):
416
441
self .drag_active = False
417
442
event .canvas .release_mouse (self .ax )
418
443
return
419
- val = self ._value_in_bounds (event .xdata )
444
+ if self .orientation is 'v' :
445
+ val = self ._value_in_bounds (event .ydata )
446
+ else :
447
+ val = self ._value_in_bounds (event .xdata )
420
448
if (val is not None ) and (val != self .val ):
421
449
self .set_val (val )
422
450
@@ -429,8 +457,12 @@ def set_val(self, val):
429
457
val : float
430
458
"""
431
459
xy = self .poly .xy
432
- xy [2 ] = val , 1
433
- xy [3 ] = val , 0
460
+ if self .orientation is 'v' :
461
+ xy [1 ] = 0 , val
462
+ xy [2 ] = 1 , val
463
+ else :
464
+ xy [2 ] = val , 1
465
+ xy [3 ] = val , 0
434
466
self .poly .xy = xy
435
467
self .valtext .set_text (self .valfmt % val )
436
468
if self .drawon :
0 commit comments