Bird
Raised Fist0
Matplotlibdata~20 mins

Interactive animation with widgets in Matplotlib - Practice Problems & Coding Challenges

Choose your learning style10 modes available

Start learning this pattern below

Jump into concepts and practice - no test required

or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Challenge - 5 Problems
🎖️
Interactive Animation Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
Output of a simple interactive sine wave plot

What is the output of this code when you move the slider to change the frequency?

Matplotlib
import numpy as np
import matplotlib.pyplot as plt
from matplotlib.widgets import Slider

x = np.linspace(0, 2 * np.pi, 400)
y = np.sin(x)

fig, ax = plt.subplots()
plt.subplots_adjust(bottom=0.25)
line, = ax.plot(x, y)
axfreq = plt.axes([0.25, 0.1, 0.65, 0.03])
freq_slider = Slider(axfreq, 'Freq', 0.1, 5.0, valinit=1)

def update(val):
    freq = freq_slider.val
    line.set_ydata(np.sin(freq * x))
    fig.canvas.draw_idle()

freq_slider.on_changed(update)
plt.show()
AA plot of a sine wave that changes frequency smoothly as the slider moves
BA static sine wave plot that does not change when the slider moves
CA plot that shows a cosine wave instead of sine wave when slider moves
DAn error occurs because Slider is not imported
Attempts:
2 left
💡 Hint

Think about what the update function does when the slider value changes.

data_output
intermediate
2:00remaining
Number of frames updated in an interactive animation

Given this interactive animation code with a slider controlling the frame index, how many frames will be updated if the slider range is from 0 to 9?

Matplotlib
import numpy as np
import matplotlib.pyplot as plt
from matplotlib.widgets import Slider

frames = [np.sin(np.linspace(0, 2 * np.pi, 100) + i) for i in range(10)]

fig, ax = plt.subplots()
line, = ax.plot(frames[0])
ax_slider = plt.axes([0.25, 0.1, 0.65, 0.03])
slider = Slider(ax_slider, 'Frame', 0, 9, valinit=0, valstep=1)

def update(val):
    frame = int(slider.val)
    line.set_ydata(frames[frame])
    fig.canvas.draw_idle()

slider.on_changed(update)
plt.show()
A100 frames will be updated because each frame has 100 points
B9 frames will be updated because the slider stops before 9
COnly 1 frame will be shown regardless of slider position
D10 frames will be updated as the slider moves from 0 to 9
Attempts:
2 left
💡 Hint

Check the slider range and how many frames are in the list.

🔧 Debug
advanced
2:00remaining
Identify the error in this interactive plot code

What error will this code raise when run?

Matplotlib
import numpy as np
import matplotlib.pyplot as plt
from matplotlib.widgets import Slider

x = np.linspace(0, 2 * np.pi, 100)
y = np.sin(x)

fig, ax = plt.subplots()
line, = ax.plot(x, y)
ax_slider = plt.axes([0.25, 0.1, 0.65, 0.03])
slider = Slider(ax_slider, 'Amplitude', 0.1, 2.0, valinit=1)

def update(val):
    amp = slider.val
    line.set_ydata(amp * np.sin(x))  # Fixed mistake here
    fig.canvas.draw_idle()

slider.on_changed(update)
plt.show()
AValueError because x and y data lengths do not match
BNo error, the plot updates correctly showing scaled sine wave
CTypeError because line.set_ydata expects array-like but gets a float array
DTypeError because multiplying float by numpy array is invalid
Attempts:
2 left
💡 Hint

Look carefully at what line.set_ydata is given compared to original y data.

visualization
advanced
2:00remaining
Effect of multiple sliders on a 2D plot

What will the plot show when two sliders control amplitude and frequency of a sine wave?

Matplotlib
import numpy as np
import matplotlib.pyplot as plt
from matplotlib.widgets import Slider

x = np.linspace(0, 2 * np.pi, 400)

fig, ax = plt.subplots()
plt.subplots_adjust(bottom=0.35)
line, = ax.plot(x, np.sin(x))

ax_amp = plt.axes([0.25, 0.2, 0.65, 0.03])
ax_freq = plt.axes([0.25, 0.1, 0.65, 0.03])

slider_amp = Slider(ax_amp, 'Amplitude', 0.1, 2.0, valinit=1)
slider_freq = Slider(ax_freq, 'Frequency', 0.1, 5.0, valinit=1)

def update(val):
    amp = slider_amp.val
    freq = slider_freq.val
    line.set_ydata(amp * np.sin(freq * x))
    fig.canvas.draw_idle()

slider_amp.on_changed(update)
slider_freq.on_changed(update)
plt.show()
AA static sine wave plot that does not respond to sliders
BTwo separate plots, one for amplitude and one for frequency
CA sine wave plot that changes amplitude and frequency interactively as sliders move
DAn error because two sliders cannot update the same plot
Attempts:
2 left
💡 Hint

Think about how the update function uses both slider values.

🚀 Application
expert
3:00remaining
Creating an interactive animation with play/pause button and slider

Which code snippet correctly implements an interactive sine wave animation with a play/pause button and a slider controlling the frame?

A
import numpy as np
import matplotlib.pyplot as plt
from matplotlib.widgets import Slider, Button

x = np.linspace(0, 2 * np.pi, 100)
frames = [np.sin(x + i * 0.1) for i in range(100)]

fig, ax = plt.subplots()
line, = ax.plot(frames[0])
ax_slider = plt.axes([0.25, 0.1, 0.65, 0.03])
slider = Slider(ax_slider, 'Frame', 0, 99, valinit=0, valstep=1)
ax_button = plt.axes([0.8, 0.025, 0.1, 0.04])
button = Button(ax_button, 'Play')

playing = False

def update(val):
    frame = int(slider.val)
    line.set_ydata(frames[frame])
    fig.canvas.draw_idle()

slider.on_changed(update)

def play(event):
    global playing
    playing = not playing
    button.label.set_text('Pause' if playing else 'Play')

import matplotlib.animation as animation

def animate(i):
    if playing:
        frame = (int(slider.val) + 1) % 100
        slider.set_val(frame)

button.on_clicked(play)
ani = animation.FuncAnimation(fig, animate, interval=100)
plt.show()
B
)(wohs.tlp
)001=lavretni ,etamina ,gif(noitaminAcnuF.noitamina = ina
)yalp(dekcilc_no.nottub

)emarf(lav_tes.redils        
001 % )1 + )lav.redils(tni( = emarf        
:gniyalp fi    
:)i(etamina fed

noitamina sa noitamina.biltolptam tropmi

)'yalP' esle gniyalp fi 'esuaP'(txet_tes.lebal.nottub    
gniyalp ton = gniyalp    
gniyalp labolg    
:)tneve(yalp fed

)etadpu(degnahc_no.redils

)(eldi_ward.savnac.gif    
)]emarf[semarf(atady_tes.enil    
)lav.redils(tni = emarf    
:)lav(etadpu fed

eslaF = gniyalp

)'yalP' ,nottub_xa(nottuB = nottub
)]40.0 ,1.0 ,520.0 ,8.0[(sexa.tlp = nottub_xa
)1=petslav ,0=tinilav ,99 ,0 ,'emarF' ,redils_xa(redilS = redils
)]30.0 ,56.0 ,1.0 ,52.0[(sexa.tlp = redils_xa
)]0[semarf(tolp.xa = ,enil
)(stolpbus.tlp = xa ,gif

])001(egnar ni i rof )1.0 * i + x(nis.pn[ = semarf
)001 ,ip.pn * 2 ,0(ecapsnil.pn = x

nottuB ,redilS tropmi stegdiw.biltolptam morf
tlp sa tolpyp.biltolptam tropmi
pn sa ypmun tropmi
C
import numpy as np
import matplotlib.pyplot as plt
from matplotlib.widgets import Slider, Button

x = np.linspace(0, 2 * np.pi, 100)
frames = [np.sin(x + i * 0.1) for i in range(100)]

fig, ax = plt.subplots()
line, = ax.plot(frames[0])
ax_slider = plt.axes([0.25, 0.1, 0.65, 0.03])
slider = Slider(ax_slider, 'Frame', 0, 99, valinit=0, valstep=1)
ax_button = plt.axes([0.8, 0.025, 0.1, 0.04])
button = Button(ax_button, 'Play')

playing = False

def update(val):
    frame = int(slider.val)
    line.set_ydata(frames[frame])
    fig.canvas.draw_idle()

slider.on_changed(update)

def play(event):
    global playing
    playing = not playing
    button.label.set_text('Pause' if playing else 'Play')

import matplotlib.animation as animation

def animate(i):
    if playing:
        frame = (int(slider.val) + 1) % 100
        line.set_ydata(frames[frame])
        slider.set_val(frame)
        fig.canvas.draw_idle()

button.on_clicked(play)
ani = animation.FuncAnimation(fig, animate, interval=100)
plt.show()
D
import numpy as np
import matplotlib.pyplot as plt
from matplotlib.widgets import Slider, Button

x = np.linspace(0, 2 * np.pi, 100)
frames = [np.sin(x + i * 0.1) for i in range(100)]

fig, ax = plt.subplots()
line, = ax.plot(frames[0])
ax_slider = plt.axes([0.25, 0.1, 0.65, 0.03])
slider = Slider(ax_slider, 'Frame', 0, 99, valinit=0, valstep=1)
ax_button = plt.axes([0.8, 0.025, 0.1, 0.04])
button = Button(ax_button, 'Play')

playing = False

def update(val):
    frame = int(slider.val)
    line.set_ydata(frames[frame])
    fig.canvas.draw_idle()

slider.on_changed(update)

def play(event):
    global playing
    playing = not playing
    button.label.set_text('Pause' if playing else 'Play')

import matplotlib.animation as animation

def animate(i):
    global playing
    if playing:
        frame = (int(slider.val) + 1) % 100
        slider.set_val(frame)

button.on_clicked(play)
ani = animation.FuncAnimation(fig, animate, interval=100)
plt.show()
Attempts:
2 left
💡 Hint

Check how the play/pause button toggles the animation and how the slider updates the frame.

Practice

(1/5)
1. What is the main purpose of using widgets like Slider in matplotlib interactive animations?
easy
A. To save the plot as an image file
B. To allow users to change plot parameters dynamically
C. To add titles and labels to the plot
D. To change the color scheme of the plot automatically

Solution

  1. Step 1: Understand the role of widgets

    Widgets like Slider let users interact with the plot by changing values live.
  2. Step 2: Identify the purpose in interactive animation

    The Slider changes parameters, triggering plot updates dynamically.
  3. Final Answer:

    To allow users to change plot parameters dynamically -> Option B
  4. Quick Check:

    Widgets enable dynamic parameter changes = A [OK]
Hint: Widgets let users control plot parameters live [OK]
Common Mistakes:
  • Thinking widgets save images
  • Confusing widgets with static labels
  • Assuming widgets change colors automatically
2. Which of the following is the correct way to import the Slider widget from matplotlib?
easy
A. from matplotlib.widgets import Slider
B. import matplotlib.slider as Slider
C. from matplotlib import Slider
D. import Slider from matplotlib.widgets

Solution

  1. Step 1: Recall matplotlib widget import syntax

    Widgets are imported from matplotlib.widgets module.
  2. Step 2: Match correct Python import statement

    The correct syntax is: from matplotlib.widgets import Slider
  3. Final Answer:

    from matplotlib.widgets import Slider -> Option A
  4. Quick Check:

    Correct import syntax = B [OK]
Hint: Widgets come from matplotlib.widgets module [OK]
Common Mistakes:
  • Using wrong import syntax
  • Trying to import Slider directly from matplotlib
  • Using JavaScript-style import
3. Given this code snippet, what will be printed when the slider value changes to 5?
import matplotlib.pyplot as plt
from matplotlib.widgets import Slider

fig, ax = plt.subplots()
plt.subplots_adjust(bottom=0.25)

slider_ax = plt.axes([0.25, 0.1, 0.65, 0.03])
slider = Slider(slider_ax, 'Value', 0, 10, valinit=0)

def update(val):
    print(f'Slider value is {val}')

slider.on_changed(update)
slider.set_val(5)
medium
A. Slider value is 0
B. No output printed
C. Slider value is 5
D. Error: update function not called

Solution

  1. Step 1: Understand slider.set_val triggers update

    Calling slider.set_val(5) changes slider value and calls update with val=5.
  2. Step 2: Check update function output

    Update prints 'Slider value is 5' when called with val=5.
  3. Final Answer:

    Slider value is 5 -> Option C
  4. Quick Check:

    set_val triggers update with new value = C [OK]
Hint: set_val calls update with new slider value [OK]
Common Mistakes:
  • Assuming initial value prints
  • Thinking update is not called automatically
  • Confusing slider value with initial valinit
4. What is wrong with this code snippet for updating a plot with a slider?
import matplotlib.pyplot as plt
from matplotlib.widgets import Slider

fig, ax = plt.subplots()
line, = ax.plot([0, 1, 2], [0, 1, 4])
slider_ax = plt.axes([0.25, 0.1, 0.65, 0.03])
slider = Slider(slider_ax, 'Scale', 0.1, 2.0, valinit=1)

def update(val):
    line.set_ydata([y * val for y in [0, 1, 4]])

slider.on_changed(update)
plt.show()
medium
A. Missing import for Slider
B. Slider range is invalid
C. Syntax error in update function
D. The plot does not update visually after slider changes

Solution

  1. Step 1: Check update function behavior

    Update changes y-data but does not redraw or refresh the plot.
  2. Step 2: Identify missing redraw call

    Missing call to redraw canvas (e.g., fig.canvas.draw_idle()) causes no visual update.
  3. Final Answer:

    The plot does not update visually after slider changes -> Option D
  4. Quick Check:

    Missing redraw causes no visual update = D [OK]
Hint: Always redraw canvas after changing plot data [OK]
Common Mistakes:
  • Forgetting fig.canvas.draw_idle() after data update
  • Assuming set_ydata auto-refreshes plot
  • Ignoring slider range correctness
5. You want to create an interactive plot where a Button resets a Slider to its initial value and updates the plot accordingly. Which of the following code snippets correctly implements this behavior?
hard
A. def reset(event): slider.set_val(slider.valinit) button.on_clicked(reset)
B. def reset(event): slider.reset(event) button.on_clicked(reset)
C. def reset(event): slider.val = slider.valinit button.on_clicked(reset)
D. def reset(event): slider.valinit = slider.val button.on_clicked(reset)

Solution

  1. Step 1: Understand slider reset methods

    Slider does not have a reset(event) method; use slider.set_val(slider.valinit) explicitly sets value and triggers update.
  2. Step 2: Check which code resets slider and triggers update

    Using slider.set_val(slider.valinit) sets slider to initial value and calls update function.
  3. Final Answer:

    def reset(event): slider.set_val(slider.valinit) button.on_clicked(reset) -> Option A
  4. Quick Check:

    Use set_val(valinit) to reset and update = A [OK]
Hint: Use slider.set_val(slider.valinit) to reset and update plot [OK]
Common Mistakes:
  • Passing event to slider.reset()
  • Setting slider.val directly without update
  • Changing valinit instead of current value