Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete 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'
Attempts:
3 left
💡 Hint
Common Mistakes
Choosing Button instead of Slider because both are widgets.
Using RadioButtons which are for selection, not sliding.
✗ Incorrect
The Slider widget from matplotlib.widgets is used to create interactive sliders for animations.
2fill in blank
mediumComplete 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'
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.
✗ Incorrect
plt.subplots() creates a figure and a set of subplots (axes) for plotting.
3fill in blank
hardFix 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'
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.
✗ Incorrect
The set_ydata method updates the y-values of the line for animation.
4fill in blank
hardFill 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'
Attempts:
3 left
💡 Hint
Common Mistakes
Using 0.2 for position which shifts slider too far right.
Labeling slider as 'Amplitude' when controlling frequency.
✗ Incorrect
The slider position starts at 0.1 on the x-axis and is labeled 'Frequency' for controlling frequency.
5fill in blank
hardFill 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'
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.
✗ Incorrect
We multiply each i by freq to get y-values, use i as loop variable, and update y-data of the line.