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

Skip to content

Commit c6e22ef

Browse files
committed
Simplify slider background rectangles
1 parent ca058d1 commit c6e22ef

File tree

1 file changed

+27
-54
lines changed

1 file changed

+27
-54
lines changed

lib/matplotlib/widgets.py

Lines changed: 27 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -269,6 +269,7 @@ def __init__(self, ax, orientation, closedmin, closedmax,
269269

270270
ax.set_axis_off()
271271
ax.set_navigate(False)
272+
272273
self.connect_event("button_press_event", self._update)
273274
self.connect_event("button_release_event", self._update)
274275
if dragging:
@@ -329,8 +330,9 @@ def __init__(self, ax, label, valmin, valmax, valinit=0.5, valfmt=None,
329330
closedmin=True, closedmax=True, slidermin=None,
330331
slidermax=None, dragging=True, valstep=None,
331332
orientation='horizontal', *, initcolor='r',
332-
handle_color_grabbed=None, handle_color_released='white',
333-
handle_edgecolor='.75', handle_size=10, **kwargs):
333+
bkd_color='grey', handle_color_grabbed=None,
334+
handle_color_released='white', handle_edgecolor='.75',
335+
handle_size=10, **kwargs):
334336
"""
335337
Parameters
336338
----------
@@ -381,6 +383,10 @@ def __init__(self, ax, label, valmin, valmax, valinit=0.5, valfmt=None,
381383
The color of the line at the *valinit* position. Set to ``'none'``
382384
for no line.
383385
386+
bkd_color : color, default: 'grey'
387+
The background color of the slider. The background is accesible for
388+
further styling via the *bkd* attribute.
389+
384390
handle_color_grabbed : color or None, default: None
385391
The color of the circular slider handle when grabbed. If *None*
386392
the same color as active part of the slider will be used.
@@ -418,26 +424,17 @@ def __init__(self, ax, label, valmin, valmax, valinit=0.5, valfmt=None,
418424
self.val = valinit
419425
self.valinit = valinit
420426
if orientation == 'vertical':
427+
self.bkd = Rectangle((.25, 0), .5, 1, transform=ax.transAxes, facecolor=bkd_color)
428+
ax.add_patch(self.bkd)
421429
self.poly = ax.axhspan(valmin, valinit, .25, .75, **kwargs)
422430
self.hline = ax.axhline(valinit, .25, .75, color=initcolor, lw=1)
423431
handleXY = [[0.5], [valinit]]
424-
self._above_rect = Rectangle(
425-
(0.25, valinit),
426-
height=valmax - valinit,
427-
width=0.5,
428-
transform=ax.get_yaxis_transform(),
429-
)
430432
else:
433+
self.bkd = Rectangle((0, .25), 1, .5, transform=ax.transAxes, facecolor=bkd_color)
434+
ax.add_patch(self.bkd)
431435
self.poly = ax.axvspan(valmin, valinit, .25, .75, **kwargs)
432436
self.vline = ax.axvline(valinit, .25, .75, color=initcolor, lw=1)
433437
handleXY = [[valinit], [0.5]]
434-
self._above_rect = Rectangle(
435-
(valinit, 0.25),
436-
width=valmax - valinit,
437-
height=0.5,
438-
transform=ax.get_xaxis_transform(),
439-
)
440-
self._above_rect.set_facecolor('grey')
441438
self._handle, = ax.plot(
442439
*handleXY,
443440
"o",
@@ -446,7 +443,6 @@ def __init__(self, ax, label, valmin, valmax, valinit=0.5, valfmt=None,
446443
markerfacecolor=handle_color_released,
447444
clip_on=False
448445
)
449-
ax.add_patch(self._above_rect)
450446

451447
self._handle_color_released = handle_color_released
452448
self._handle_edgecolor = handle_edgecolor
@@ -551,14 +547,10 @@ def set_val(self, val):
551547
if self.orientation == 'vertical':
552548
xy[1] = .25, val
553549
xy[2] = .75, val
554-
self._above_rect.set_y(val)
555-
self._above_rect.set_height(self.valmax-val)
556550
self._handle.set_ydata([val])
557551
else:
558552
xy[2] = val, .75
559553
xy[3] = val, .25
560-
self._above_rect.set_x(val)
561-
self._above_rect.set_width(self.valmax-val)
562554
self._handle.set_xdata([val])
563555
self.poly.xy = xy
564556
self.valtext.set_text(self._format(val))
@@ -614,6 +606,7 @@ def __init__(
614606
dragging=True,
615607
valstep=None,
616608
orientation="horizontal",
609+
bkd_color='grey',
617610
handle_color_grabbed=None,
618611
handle_color_released='white',
619612
handle_edgecolor='.75',
@@ -658,6 +651,10 @@ def __init__(
658651
orientation : {'horizontal', 'vertical'}, default: 'horizontal'
659652
The orientation of the slider.
660653
654+
bkd_color : color, default: 'grey'
655+
The background color of the slider. The background is accesible for
656+
further styling via the *bkd* attribute.
657+
661658
handle_color_grabbed : color or None, default: None
662659
The color of the circular slider handles when grabbed. If *None*
663660
the same color as active part of the slider will be used.
@@ -692,41 +689,23 @@ def __init__(
692689
self.val = valinit
693690
self.valinit = valinit
694691
if orientation == "vertical":
692+
self.bkd = Rectangle((.25, 0), .5, 2,
693+
transform=ax.transAxes,
694+
facecolor=bkd_color
695+
)
696+
ax.add_patch(self.bkd)
695697
self.poly = ax.axhspan(valinit[0], valinit[1], 0, 1, **kwargs)
696698
handleXY_1 = [.5, valinit[0]]
697699
handleXY_2 = [.5, valinit[1]]
698-
self._above_rect = Rectangle(
699-
(.25, valinit[1]),
700-
height=valmax - valinit[1],
701-
width=0.5,
702-
transform=ax.get_yaxis_transform(),
703-
)
704-
self._below_rect = Rectangle(
705-
(valmin, 0.25),
706-
width=valinit[0] - valmin,
707-
height=0.5,
708-
transform=ax.get_xaxis_transform(),
709-
)
710700
else:
701+
self.bkd = Rectangle((0, .25), 1, .5,
702+
transform=ax.transAxes,
703+
facecolor=bkd_color
704+
)
705+
ax.add_patch(self.bkd)
711706
self.poly = ax.axvspan(valinit[0], valinit[1], 0, 1, **kwargs)
712707
handleXY_1 = [valinit[0], .5]
713708
handleXY_2 = [valinit[1], .5]
714-
self._above_rect = Rectangle(
715-
(valinit[1], 0.25),
716-
width=valmax - valinit[1],
717-
height=0.5,
718-
transform=ax.get_xaxis_transform(),
719-
)
720-
self._below_rect = Rectangle(
721-
(valmin, 0.25),
722-
width=valinit[0] - valmin,
723-
height=0.5,
724-
transform=ax.get_xaxis_transform(),
725-
)
726-
self._above_rect.set_facecolor('grey')
727-
self._below_rect.set_facecolor('grey')
728-
ax.add_patch(self._above_rect)
729-
ax.add_patch(self._below_rect)
730709
self._handles = [
731710
ax.plot(
732711
*handleXY_1,
@@ -934,18 +913,12 @@ def set_val(self, val):
934913
xy[2] = .75, val[1]
935914
xy[3] = .75, val[0]
936915
xy[4] = .25, val[0]
937-
self._above_rect.set_height(self.valmax - val[1])
938-
self._above_rect.set_y(val[1])
939-
self._below_rect.set_height(val[0] - self.valmin)
940916
else:
941917
xy[0] = val[0], .25
942918
xy[1] = val[0], .75
943919
xy[2] = val[1], .75
944920
xy[3] = val[1], .25
945921
xy[4] = val[0], .25
946-
self._above_rect.set_width(self.valmax - val[1])
947-
self._above_rect.set_x(val[1])
948-
self._below_rect.set_width(val[0] - self.valmin)
949922
self.poly.xy = xy
950923
self.valtext.set_text(self._format(val))
951924
if self.drawon:

0 commit comments

Comments
 (0)