Challenge - 5 Problems
Interactive Data Explorer
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
Output of interactive matplotlib plot code
What will be the output when running this code snippet that uses matplotlib's interactive features?
Matplotlib
import matplotlib.pyplot as plt import numpy as np x = np.linspace(0, 10, 100) y = np.sin(x) fig, ax = plt.subplots() line, = ax.plot(x, y) plt.ion() plt.show() for phase in np.linspace(0, 10, 100): line.set_ydata(np.sin(x + phase)) fig.canvas.draw() fig.canvas.flush_events()
Attempts:
2 left
💡 Hint
Think about what plt.ion() does and how the loop updates the plot.
✗ Incorrect
plt.ion() enables interactive mode, allowing the plot to update inside the loop. The line's y-data changes, and the canvas redraws, creating an animation effect.
❓ data_output
intermediate2:00remaining
Data output from interactive slider in matplotlib
Given this code using matplotlib's Slider widget, what is the printed output when the slider is moved to 0.5?
Matplotlib
import matplotlib.pyplot as plt from matplotlib.widgets import Slider fig, ax = plt.subplots() plt.subplots_adjust(bottom=0.25) x = [0, 1, 2, 3, 4] y = [0, 1, 4, 9, 16] line, = ax.plot(x, y) ax_slider = plt.axes([0.25, 0.1, 0.65, 0.03]) slider = Slider(ax_slider, 'Scale', 0.1, 2.0, valinit=1) def update(val): scale = slider.val new_y = [i * scale for i in y] line.set_ydata(new_y) print(new_y) fig.canvas.draw_idle() slider.on_changed(update) plt.show()
Attempts:
2 left
💡 Hint
Multiply each y value by 0.5 exactly, including floats.
✗ Incorrect
Each y value is multiplied by the slider value 0.5, producing floats. So 0*0.5=0.0, 1*0.5=0.5, 4*0.5=2.0, 9*0.5=4.5, 16*0.5=8.0.
❓ visualization
advanced2:00remaining
Effect of interactivity on data exploration
Which visualization best demonstrates how interactivity helps explore data trends dynamically?
Matplotlib
import matplotlib.pyplot as plt import numpy as np from matplotlib.widgets import Slider x = np.linspace(0, 10, 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, 'Frequency', 0.1, 5.0, valinit=1) def update(val): freq = slider.val line.set_ydata(np.sin(freq * x)) fig.canvas.draw_idle() slider.on_changed(update) plt.show()
Attempts:
2 left
💡 Hint
Look for the option describing a slider controlling the plot.
✗ Incorrect
The code creates a sine wave plot with a slider that changes frequency, allowing dynamic exploration of how frequency affects the wave shape.
🔧 Debug
advanced2:00remaining
Identify the error in interactive matplotlib code
What error will this code produce when run?
Matplotlib
import matplotlib.pyplot as plt import numpy as np x = np.linspace(0, 10, 100) y = np.cos(x) fig, ax = plt.subplots() line, = ax.plot(x, y) plt.ion() plt.show() for phase in np.linspace(0, 10, 100): line.set_ydata(np.cos(x + phase)) fig.canvas.draw() fig.canvas.flush_events()
Attempts:
2 left
💡 Hint
Consider how plt.show() behaves in interactive mode without flushing events.
✗ Incorrect
Without calling fig.canvas.flush_events(), the GUI event loop may not process updates properly, causing a RuntimeError about the main thread.
🚀 Application
expert2:00remaining
Why interactivity enhances data exploration in matplotlib
Which statement best explains why adding interactivity to matplotlib plots helps data exploration?
Attempts:
2 left
💡 Hint
Think about how changing plot settings on the fly helps understand data better.
✗ Incorrect
Interactivity lets users adjust parameters like scale, frequency, or filters live, helping them discover trends and insights that static plots hide.