matplotlib.widgets.Slider#

classmatplotlib.widgets.Slider(ax,label,valmin,valmax,*,valinit=0.5,valfmt=None,closedmin=True,closedmax=True,slidermin=None,slidermax=None,dragging=True,valstep=None,orientation='horizontal',initcolor='r',track_color='lightgrey',handle_style=None,**kwargs)[source]#

Bases:SliderBase

A slider representing a floating point range.

Create a slider fromvalmin tovalmax in Axesax. For the slider toremain responsive you must maintain a reference to it. Callon_changed() to connect to the slider event.

Attributes:
valfloat

Slider value.

Parameters:
axAxes

The Axes to put the slider in.

labelstr

Slider label.

valminfloat

The minimum value of the slider.

valmaxfloat

The maximum value of the slider.

valinitfloat, default: 0.5

The slider initial position.

valfmtstr, default: None

The way to format the slider value. If a string, it must be in %-format.If a callable, it must have the signaturevalfmt(val:float)->str.If None, aScalarFormatter is used.

closedminbool, default: True

Whether the slider interval is closed on the bottom.

closedmaxbool, default: True

Whether the slider interval is closed on the top.

sliderminSlider, default: None

Do not allow the current slider to have a value less thanthe value of the Sliderslidermin.

slidermaxSlider, default: None

Do not allow the current slider to have a value greater thanthe value of the Sliderslidermax.

draggingbool, default: True

If True the slider can be dragged by the mouse.

valstepfloat or array-like, default: None

If a float, the slider will snap to multiples ofvalstep.If an array the slider will snap to the values in the array.

orientation{'horizontal', 'vertical'}, default: 'horizontal'

The orientation of the slider.

initcolorcolor, default: 'r'

The color of the line at thevalinit position. Set to'none'for no line.

track_colorcolor, default: 'lightgrey'

The color of the background track. The track is accessible forfurther styling via thetrack attribute.

handle_styledict

Properties of the slider handle. Default values are

Key

Value

Default

Description

facecolor

color

'white'

The facecolor of the slider handle.

edgecolor

color

'.75'

The edgecolor of the slider handle.

size

int

10

The size of the slider handle in points.

Other values will be transformed as marker{foo} and passed to theLine2D constructor. e.g.handle_style={'style'='x'} willresult inmarkerstyle='x'.

Notes

Additional kwargs are passed on toself.poly which is theRectangle that draws the slider knob. See theRectangle documentation for valid property names (facecolor,edgecolor,alpha, etc.).

on_changed(func)[source]#

Connectfunc as callback function to changes of the slider value.

Parameters:
funccallable

Function to call when slider is changed.The function must accept a single float as its arguments.

Returns:
int

Connection id (which can be used to disconnectfunc).

set_val(val)[source]#

Set slider value toval.

Parameters:
valfloat

Examples usingmatplotlib.widgets.Slider#

Slider

Slider

Snap sliders to discrete values

Snap sliders to discrete values