0
0
Matplotlibdata~10 mins

Interactive animation with widgets in Matplotlib - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to import the widget module needed for interactive sliders.

Matplotlib
from matplotlib import pyplot as plt
from matplotlib import animation
from matplotlib.widgets import [1]
Drag options to blanks, or click blank then click option'
ASlider
BButton
CCheckButtons
DRadioButtons
Attempts:
3 left
💡 Hint
Common Mistakes
Choosing Button instead of Slider because both are widgets.
Using RadioButtons which are for selection, not sliding.
2fill in blank
medium

Complete the code to create a figure and axis for plotting.

Matplotlib
fig, ax = plt.[1]()
Drag options to blanks, or click blank then click option'
Aplot
Bshow
Cfigure
Dsubplots
Attempts:
3 left
💡 Hint
Common Mistakes
Using plt.figure() which returns only the figure, not axes.
Using plt.plot() which creates a plot but not figure and axes objects.
3fill in blank
hard

Fix the error in the animation function to update the line data.

Matplotlib
def update(frame):
    y = [i * frame for i in range(10)]
    line.set_[1](y)
    return line,
Drag options to blanks, or click blank then click option'
Aupdate_data
Bset_data
Cset_ydata
Dset_xdata
Attempts:
3 left
💡 Hint
Common Mistakes
Using set_data which requires both x and y data.
Using set_xdata which updates x-values, not y-values.
4fill in blank
hard

Fill both blanks to create a Slider widget and set its position on the figure.

Matplotlib
ax_slider = plt.axes([[1], 0.05, 0.65, 0.03])
slider = Slider(ax_slider, '[2]', 0, 10, valinit=5)
Drag options to blanks, or click blank then click option'
A0.1
B0.2
CFrequency
DAmplitude
Attempts:
3 left
💡 Hint
Common Mistakes
Using 0.2 for position which shifts slider too far right.
Labeling slider as 'Amplitude' when controlling frequency.
5fill in blank
hard

Fill all three blanks to update the animation frame using slider value and redraw the line.

Matplotlib
def update(val):
    freq = slider.val
    y = [[1] * freq for [2] in range(10)]
    line.set_[3](y)
    fig.canvas.draw_idle()
Drag options to blanks, or click blank then click option'
Ai
Bi * 2
Cset_ydata
Dset_data
Attempts:
3 left
💡 Hint
Common Mistakes
Using set_data which requires both x and y data.
Using i * 2 instead of i for the loop variable.