0
0
Matplotlibdata~10 mins

Widget-based interactions (sliders, buttons) 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 slider widget from matplotlib.

Matplotlib
from matplotlib.widgets import [1]
Drag options to blanks, or click blank then click option'
AButton
BSlider
CRadioButtons
DCheckButtons
Attempts:
3 left
💡 Hint
Common Mistakes
Importing Button instead of Slider
Using RadioButtons or CheckButtons which are different widgets
2fill in blank
medium

Complete the code to create a slider with label 'Amplitude' and range from 0 to 10.

Matplotlib
slider = Slider(ax=[1], label='Amplitude', valmin=0, valmax=10, valinit=5)
Drag options to blanks, or click blank then click option'
Aplt.axes([0.25, 0.1, 0.65, 0.03])
Bplt.plot()
Cplt.figure()
Dplt.subplot()
Attempts:
3 left
💡 Hint
Common Mistakes
Using plt.figure() which creates a whole figure, not an axes
Using plt.plot() which creates a plot line, not an axes
Using plt.subplot() which is for subplots, not slider axes
3fill in blank
hard

Fix the error in the slider update function to correctly update the plot line's y-data.

Matplotlib
def update(val):
    amp = slider.val
    line.set_ydata([1])
    fig.canvas.draw_idle()
Drag options to blanks, or click blank then click option'
Aamp + np.sin(x)
Bnp.sin(x)
Camp / np.sin(x)
Damp * np.sin(x)
Attempts:
3 left
💡 Hint
Common Mistakes
Not multiplying by amp, so the slider has no effect
Adding or dividing by amp which changes the wave incorrectly
4fill in blank
hard

Fill both blanks to create a button that resets the slider to its initial value when clicked.

Matplotlib
button = Button(ax=[1], label='Reset')

def reset(event):
    slider.[2]()
Drag options to blanks, or click blank then click option'
Aplt.axes([0.8, 0.025, 0.1, 0.04])
Bplt.axes([0.1, 0.025, 0.1, 0.04])
Creset
Dreset_val()
Attempts:
3 left
💡 Hint
Common Mistakes
Using wrong axes position for the button
Calling a non-existent method like reset_val()
5fill in blank
hard

Fill both blanks to connect the slider and button widgets to update and reset the plot interactively.

Matplotlib
slider.on_changed([1])
button.on_clicked([2])
plt.show()  # Display the interactive plot
Drag options to blanks, or click blank then click option'
Aupdate
Breset
Cupdate()
Dreset()
Attempts:
3 left
💡 Hint
Common Mistakes
Using parentheses when passing functions as callbacks
Mixing up update and reset functions