Matplotlib provides several widgets to make interactive plots. Among these widgets, the Slider widget is discussed here. The Slider provides control over the visual properties of the plot. Slider() is used to place a slider representing a floating point range in a plot on provided axes.
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’, **kwargs)
- ax: A matplotlib.axes.Axes instance where a slider is placed
- label: Slider text label
- valmin: The minimum value of the slider
- valmax: The maximum value of the slider
- valinit: Initial Value of a slider. Default value is 0.5.
- valfmt: Slider value format string (%-format). Default value is None. If None, a ScalarFormatter is used.
- closedmin: Slider interval is closed on bottom or not.
- closedmax: Slider interval is closed on the top or not.
- slidermin: Forbid current slider to have value less than current value of the given slider. Default value is None.
- slidermax: Forbid current slider to have value greater than current value of the given slider. Default value is None.
- dragging: The slider can be dragged by mouse or not. Default value is True (slider can be dragged by mouse)
- valstep: The slider will slides at values in multiples of valstep value. Default value is None.
- orientation: The slider orientation, vertical or horizontal . Default value is horizontal.
- kwargs are related to Rectangle that draws the slider knob. Valid properties such as facecolor, edgecolor, alpha, etc. of Matplotlib.patches.Rectangle can be used here.
- disconnect(self, cid): Removes the observer with connection id cid
- on_changed(self, func): To connect to the slider event. When the slider value is changed, respective function func is called. Func takes a new slider value as argument and returns connection id.
- reset(self): The slider value is set to the initial value
- set_val(self, val): Sets slider value to val
Following example demonstrates the change in color of bar chart using reg, green, blue value sliders.
In this example, a slider are used to change the frequency and amplitude of a sine wave
Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.
To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course.