What is the output of this code when you move the slider to change the frequency?
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()
Think about what the update function does when the slider value changes.
The slider changes the frequency value, and the update function updates the sine wave's y-data accordingly, so the plot changes smoothly.
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?
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()
Check the slider range and how many frames are in the list.
The slider ranges from 0 to 9 with step 1, so it can select all 10 frames indexed 0 through 9.
What error will this code raise when run?
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()
Look carefully at what line.set_ydata is given compared to original y data.
The original y data is sine values, but the update sets y data to amp * np.sin(x), which is the correct scaled sine wave. This does not cause an error and updates the plot correctly.
What will the plot show when two sliders control amplitude and frequency of a sine wave?
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()
Think about how the update function uses both slider values.
Both sliders control parameters of the sine wave. The update function uses both values to update the plot dynamically.
Which code snippet correctly implements an interactive sine wave animation with a play/pause button and a slider controlling the frame?
Check how the play/pause button toggles the animation and how the slider updates the frame.
Option A correctly toggles the playing state, updates the slider value to advance frames, and the slider's on_changed updates the plot. It avoids redundant plot updates inside animate.