0
0
Matplotlibdata~20 mins

Highlight and annotate pattern in Matplotlib - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Highlight and Annotate Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
Output of highlighted region with annotation
What will be the output of this code snippet that highlights a region and adds an annotation on a matplotlib plot?
Matplotlib
import matplotlib.pyplot as plt
import numpy as np
x = np.linspace(0, 10, 100)
y = np.sin(x)
plt.plot(x, y)
plt.axvspan(2, 4, color='yellow', alpha=0.3)
plt.annotate('Peak region', xy=(3, np.sin(3)), xytext=(5, 0.5), arrowprops=dict(facecolor='black', shrink=0.05))
plt.show()
AA sine wave plot with no shaded region and an annotation text 'Peak region' at (3, sin(3)) without arrow.
BA sine wave plot with a red shaded horizontal band between y=2 and y=4 and an arrow pointing from (3, sin(3)) to (5, 0.5) labeled 'Peak region'.
CA sine wave plot with a yellow shaded vertical band between x=3 and x=5 and an annotation text 'Peak region' at (2, 0.5) without arrow.
DA sine wave plot with a yellow shaded vertical band between x=2 and x=4 and an arrow pointing from (5, 0.5) to (3, sin(3)) labeled 'Peak region'.
Attempts:
2 left
💡 Hint
Look at the parameters of axvspan and annotate carefully, especially the coordinates and colors.
data_output
intermediate
1:30remaining
Number of annotations and highlighted regions
Given the following code, how many highlighted regions and annotations will appear on the plot?
Matplotlib
import matplotlib.pyplot as plt
x = range(10)
y = [i**2 for i in x]
plt.plot(x, y)
plt.axvspan(2, 4, color='green', alpha=0.2)
plt.axvspan(6, 8, color='red', alpha=0.2)
plt.annotate('Low range', xy=(3, 9), xytext=(1, 20), arrowprops=dict(facecolor='black'))
plt.annotate('High range', xy=(7, 49), xytext=(8, 80), arrowprops=dict(facecolor='black'))
plt.show()
A2 highlighted vertical regions and 2 annotations with arrows.
B1 highlighted vertical region and 2 annotations with arrows.
C2 highlighted vertical regions and 1 annotation with arrow.
DNo highlighted regions and 2 annotations without arrows.
Attempts:
2 left
💡 Hint
Count the number of axvspan and annotate calls with arrowprops.
🔧 Debug
advanced
1:30remaining
Identify the error in annotation coordinates
What error will this code produce when run, and why?
Matplotlib
import matplotlib.pyplot as plt
x = [1, 2, 3]
y = [4, 5, 6]
plt.plot(x, y)
plt.annotate('Point', xy=(4, 5), xytext=(2, 6), arrowprops=dict(facecolor='blue'))
plt.show()
AValueError because xy coordinate (4,5) is outside the plot data range.
BRuntimeWarning about annotation coordinates being outside the axes limits.
CNo error; the plot shows with annotation arrow pointing from (2,6) to (4,5).
DSyntaxError due to incorrect annotate parameters.
Attempts:
2 left
💡 Hint
Check if matplotlib allows annotations outside the data range.
visualization
advanced
1:30remaining
Visual effect of alpha in highlighted region
What visual difference will changing alpha from 0.1 to 0.8 in axvspan produce on the plot?
Matplotlib
import matplotlib.pyplot as plt
import numpy as np
x = np.linspace(0, 5, 100)
y = np.cos(x)
plt.plot(x, y)
plt.axvspan(1, 3, color='blue', alpha=0.1)
plt.show()
AThe blue shaded region color will change to green with alpha=0.8.
BThe blue shaded region will become much more opaque and darker with alpha=0.8 compared to alpha=0.1.
CThe blue shaded region will become lighter and more transparent with alpha=0.8 compared to alpha=0.1.
DNo visible change occurs when changing alpha from 0.1 to 0.8.
Attempts:
2 left
💡 Hint
Alpha controls transparency; higher alpha means less transparent.
🚀 Application
expert
3:00remaining
Highlight and annotate multiple peaks in data
Given a noisy sine wave, which code snippet correctly highlights the peaks between x=1 to 2 and x=4 to 5 with different colors and annotates them with arrows?
Matplotlib
import matplotlib.pyplot as plt
import numpy as np
x = np.linspace(0, 6, 300)
y = np.sin(x) + 0.1 * np.random.randn(300)
plt.plot(x, y)
# Highlight and annotate peaks here
plt.show()
A
plt.axvspan(1, 2, color='blue', alpha=0.5)
plt.axvspan(4, 5, color='yellow', alpha=0.5)
plt.annotate('Peak 1', xy=(1.5, y[75]), xytext=(0.5, 1), arrowprops=dict(facecolor='blue'))
plt.annotate('Peak 2', xy=(4.5, y[224]), xytext=(5.5, 1), arrowprops=dict(facecolor='yellow'))
B
plt.axvspan(1, 2, color='red', alpha=0.3)
plt.axvspan(4, 5, color='green', alpha=0.3)
plt.annotate('Peak 1', xy=(1.5, np.sin(1.5)), xytext=(0.5, 1), arrowprops=dict(facecolor='red'))
plt.annotate('Peak 2', xy=(4.5, np.sin(4.5)), xytext=(5.5, 1), arrowprops=dict(facecolor='green'))
C
plt.axhspan(1, 2, color='red', alpha=0.3)
plt.axhspan(4, 5, color='green', alpha=0.3)
plt.annotate('Peak 1', xy=(1.5, np.sin(1.5)), xytext=(0.5, 1), arrowprops=dict(facecolor='red'))
plt.annotate('Peak 2', xy=(4.5, np.sin(4.5)), xytext=(5.5, 1), arrowprops=dict(facecolor='green'))
D
plt.axvspan(1, 2, color='red', alpha=0.3)
plt.axvspan(4, 5, color='green', alpha=0.3)
plt.annotate('Peak 1', xy=(1.5, y[100]), xytext=(0.5, 1), arrowprops=dict(facecolor='blue'))
plt.annotate('Peak 2', xy=(4.5, y[200]), xytext=(5.5, 1), arrowprops=dict(facecolor='green'))
Attempts:
2 left
💡 Hint
Use the noisy y values for annotation coordinates to match the plotted data points.