matplotlib.widgets.Slider#

class matplotlib.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 from valmin to valmax in Axes ax. For the slider to remain responsive you must maintain a reference to it. Call on_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 signature valfmt(val: float) -> str. If None, a ScalarFormatter 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 than the value of the Slider slidermin.

slidermaxSlider, default: None

Do not allow the current slider to have a value greater than the value of the Slider slidermax.

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 of valstep. 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 the valinit position. Set to 'none' for no line.

track_colorcolor, default: 'lightgrey'

The color of the background track. The track is accessible for further styling via the track 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 the Line2D constructor. e.g. handle_style = {'style'='x'} will result in markerstyle = 'x'.

Notes

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

on_changed(func)[source]#

Connect func 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 disconnect func).

set_val(val)[source]#

Set slider value to val.

Parameters:
valfloat

Examples using matplotlib.widgets.Slider#

Slider

Slider

Snap sliders to discrete values

Snap sliders to discrete values