Skip to content
Related Articles

Related Articles

Improve Article
Save Article
Like Article

Add a vertical slider with matplotlib

  • Last Updated : 17 May, 2021

Matplotlib not only allows static graphs, but we can also prepare plots that can be modified interactively.  For this, we can use the Sliders widget present in the widgets submodule to control the visual properties of your plot.

The only difference between horizontal and vertical Sliders being the presence of an additional parameter ‘orientation’ which is by default set to ‘horizontal’.

amp_slider = Slider(






   orientation=”vertical” # Update it to “horizontal” if you need a horizontal graph



Here we will use the Slider widget to create a plot of a function with a scroll bar that can be used to modify the plot. In this example, two sliders (one vertical and one horizontal )are being used to choose the amplitude and frequencies of a sine wave. We can control many continuously-varying properties of our plot in this way.


import numpy as np
import matplotlib.pyplot as plt
from matplotlib.widgets import Slider, Button
# The parameterized function to be plotted
def f(t, amplitude, frequency):
    return amplitude * np.sin(2 * np.pi * frequency * t)
t = np.linspace(0, 1, 1000)
# Defining the initial parameters
init_amplitude = 5
init_frequency = 3
# Creating the figure and the graph line that we will update
fig, ax = plt.subplots()
line, = plt.plot(t, f(t, init_amplitude, init_frequency), lw=2)
ax.set_xlabel('Time [s]')
axcolor = 'lightgoldenrodyellow'
# adjusting the main plot to make space for our sliders
plt.subplots_adjust(left=0.25, bottom=0.25)
# Making a horizontally oriented slider to
# control the frequency.
axfreq = plt.axes([0.25, 0.1, 0.65, 0.03], facecolor=axcolor)
freq_slider = Slider(
    label='Frequency [Hz]',
    # orientation="horizontal" is Default
# Making a vertically oriented slider to control the amplitude
axamp = plt.axes([0.1, 0.25, 0.0225, 0.63], facecolor=axcolor)
amp_slider = Slider(
# Function to be rendered anytime a slider's value changes
def update(val):
    line.set_ydata(f(t, amp_slider.val, freq_slider.val))
# Registering the update function with each slider Update
# Create a `matplotlib.widgets.Button` to reset
# the sliders to initial parameters.
resetax = plt.axes([0.8, 0.025, 0.1, 0.04])
button = Button(resetax, 'Reset', color=axcolor, hovercolor='0.975')
def reset(event):

Output :

Continuously-varying properties like Amplitude and Frequency of a Sine Wve could be controlled using Sliders effectively :

  As we can observe from example images the plot can be modified during runtime by using the Sliders submodule present in Matplotlib.

Therefore, the only difference between the horizontal and vertical Sliders is the presence of an additional parameter ‘orientation’ which is by default set to ‘horizontal’ and there is no difference in implementation of the orientation you want.

My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!