Interactive Plotting with Matplotlib: Creating a Frequency Slider
In this blog, we will explore how to create an interactive plot using Matplotlib and a slider widget. This example demonstrates the use of the Slider widget to modify the frequency of a sine wave in real time.
Get Someone’s stories in your inbox
Join Medium for free to get updates from this writer.
It’s a fantastic way to visualize how changing parameters can affect a function.
import matplotlib.pyplot as plt
import numpy as np
from matplotlib.widgets import Slider
fig, ax = plt.subplots()
plt.subplots_adjust(left=0.1, bottom=0.25)
x = np.linspace(0, 2 * np.pi, 1000)
initial_frequency = 1
y = np.sin(initial_frequency * x)
line, = plt.plot(x, y, lw=2)
ax_slider = plt.axes([0.1, 0.1, 0.65, 0.03])
frequency_slider = Slider(ax_slider, 'Frequency', 0.1, 5.0, valinit=initial_frequency)
def update(val):
frequency = frequency_slider.val
line.set_ydata(np.sin(frequency * x))
fig.canvas.draw_idle()
frequency_slider.on_changed(update)
plt.show()
plt.clf()Let’s break down the code step-by-step.
Importing Necessary Libraries
import matplotlib.pyplot as plt
import numpy as np
from matplotlib.widgets import Slider- Matplotlib: A powerful plotting library for creating visualizations in Python.
- NumPy: A library for numerical operations in Python, particularly useful for handling data in arrays.
- Slider: A widget from matplotlib.widgets that allows you to create interactive controls on your plots.
Creating the Figure and Axis
fig, ax = plt.subplots()
plt.subplots_adjust(left=0.1, bottom=0.25)- fig, ax = plt.subplots(): This creates a figure and a single set of axes on which we’ll plot the sine wave.
- plt.subplots_adjust(): Adjusts the position of the plot within the figure to make room for the slider.
Generating Data for the Sine Wave
x = np.linspace(0, 2 * np.pi, 1000)
initial_frequency = 1
y = np.sin(initial_frequency * x)- np.linspace(): Generates evenly spaced values over a specified interval. In this case, it creates 1000 points between 0 and 2π.
- initial_frequency: The initial frequency of the sine wave, which we’ll adjust with the slider.
- y = np.sin(initial_frequency * x): Calculates the sine of x, with the initial frequency.
Plotting the Initial Sine Wave
line, = plt.plot(x, y, lw=2)- This line plots the sine wave on the axes with a linewidth of 2.
- The line object will be used later to update the sine wave when the slider value changes.
Creating the Slider Widget
ax_slider = plt.axes([0.1, 0.1, 0.65, 0.03])
frequency_slider = Slider(ax_slider, 'Frequency', 0.1, 5.0, valinit=initial_frequency)- ax_slider: Defines the position of the slider within the plot area.
- Slider(): Initializes the slider with a label Frequency, a minimum value of 0.1, a maximum value of 5.0, and an initial value matching the starting frequency.
Updating the Plot with the Slider
def update(val):
frequency = frequency_slider.val
line.set_ydata(np.sin(frequency * x))
fig.canvas.draw_idle()- update(val): This function recalculates the sine wave using the new frequency value from the slider and updates the plot.
- fig.canvas.draw_idle(): Efficiently updates the figure to reflect the changes without a full redraw.
Connecting the Slider to the Update Function
frequency_slider.on_changed(update)- This line ensures that any movement of the slider will trigger the update function, allowing the plot to change dynamically.
Displaying the Interactive Plot
plt.show()
plt.clf()- plt.show(): Displays the plot in a window.
- plt.clf(): Clears the figure after the plot window is closed, to free up memory.
Visualization of the Interactive Sine Wave Plot
Conclusion
In this blog, we’ve created a dynamic visualization using Matplotlib that allows users to interactively change the frequency of a sine wave using a slider. This approach can be extended to other parameters and types of plots, providing a powerful tool for data analysis and presentation.
Feel free to explore and modify the code to suit your specific needs. You can find more of my code examples on my Github and stay updated with my progress on Twitter.
This blog marks my 36th milestone in my journey of mastering Matplotlib. Stay tuned for more advanced visualizations!









