Bird
Raised Fist0
Matplotlibdata~20 mins

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

Choose your learning style10 modes available

Start learning this pattern below

Jump into concepts and practice - no test required

or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
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.

Practice

(1/5)
1. What is the main purpose of using highlight and annotate in a matplotlib plot?
easy
A. To save the plot as an image file
B. To change the color scheme of the entire plot
C. To draw attention to important parts and add notes explaining data points
D. To remove grid lines from the plot

Solution

  1. Step 1: Understand the role of highlight

    Highlighting is used to emphasize important areas on a graph to make them stand out.
  2. Step 2: Understand the role of annotate

    Annotations add notes with arrows to explain or give more information about specific data points.
  3. Final Answer:

    To draw attention to important parts and add notes explaining data points -> Option C
  4. Quick Check:

    Highlight + annotate = emphasize + explain [OK]
Hint: Highlight = focus, annotate = explain with notes [OK]
Common Mistakes:
  • Thinking highlight changes entire plot color
  • Confusing annotate with saving files
  • Assuming highlight removes grid lines
2. Which of the following is the correct syntax to add an annotation with an arrow pointing to point (2, 4) in matplotlib?
easy
A. plt.annotate('Note', xy=(2, 4), xytext=(3, 5), arrowprops=dict(facecolor='black'))
B. plt.annotate('Note', point=(2, 4), text=(3, 5), arrow=True)
C. plt.annotation('Note', xy=(2, 4), xytext=(3, 5), arrowprops=True)
D. plt.annotate('Note', xy=(2, 4), text=(3, 5), arrowprops=dict(color='red'))

Solution

  1. Step 1: Check the function name and parameters

    The correct function is plt.annotate with parameters xy for point and xytext for text location.
  2. Step 2: Verify arrow properties

    arrowprops must be a dictionary specifying arrow style, e.g., dict(facecolor='black').
  3. Final Answer:

    plt.annotate('Note', xy=(2, 4), xytext=(3, 5), arrowprops=dict(facecolor='black')) -> Option A
  4. Quick Check:

    Correct annotate syntax uses xy, xytext, arrowprops dict [OK]
Hint: Use xy and xytext with arrowprops dict for annotation [OK]
Common Mistakes:
  • Using wrong parameter names like point or text
  • Passing arrowprops as True instead of dict
  • Using plt.annotation instead of plt.annotate
3. What will be the output of this code snippet?
import matplotlib.pyplot as plt
x = [1, 2, 3, 4]
y = [10, 20, 25, 30]
plt.plot(x, y)
plt.axvspan(2, 3, color='yellow', alpha=0.3)
plt.annotate('Peak', xy=(3, 25), xytext=(3.5, 27), arrowprops=dict(facecolor='blue'))
plt.show()
medium
A. A line plot with a red shaded area between x=2 and x=3 and no annotation
B. A line plot with a yellow shaded area between x=2 and x=3 and an annotation 'Peak' pointing at (3, 25)
C. A scatter plot with points highlighted in yellow and annotation at (3.5, 27)
D. A line plot with no shading and annotation text 'Peak' at (3, 25) without arrow

Solution

  1. Step 1: Understand plt.axvspan usage

    plt.axvspan(2, 3, color='yellow', alpha=0.3) creates a translucent yellow vertical highlight between x=2 and x=3.
  2. Step 2: Understand plt.annotate usage

    plt.annotate('Peak', xy=(3, 25), xytext=(3.5, 27), arrowprops=dict(facecolor='blue')) adds an annotation 'Peak' with an arrow pointing at (3, 25).
  3. Final Answer:

    A line plot with a yellow shaded area between x=2 and x=3 and an annotation 'Peak' pointing at (3, 25) -> Option B
  4. Quick Check:

    axvspan = highlight, annotate = note with arrow [OK]
Hint: axvspan highlights vertical area, annotate adds arrow note [OK]
Common Mistakes:
  • Confusing axvspan color or range
  • Thinking annotation text appears without arrow
  • Mistaking line plot for scatter plot
4. Identify the error in this code that tries to highlight and annotate a point:
import matplotlib.pyplot as plt
x = [1, 2, 3]
y = [5, 7, 9]
plt.plot(x, y)
plt.axhspan(6, 8, color='green')
plt.annotate('Important', xy=(2, 7), xytext=(2, 9), arrowprops='->')
plt.show()
medium
A. plt.axhspan should use x values, not y values
B. plt.plot should use scatter instead for annotation
C. xytext coordinates must be inside the plot range
D. arrowprops should be a dictionary, not a string

Solution

  1. Step 1: Check plt.axhspan usage

    plt.axhspan(6, 8, color='green') is correct to highlight horizontal area between y=6 and y=8.
  2. Step 2: Check plt.annotate arrowprops parameter

    arrowprops must be a dictionary describing arrow style, not a string like '->'.
  3. Final Answer:

    arrowprops should be a dictionary, not a string -> Option D
  4. Quick Check:

    arrowprops = dict(...) not string [OK]
Hint: arrowprops needs dict, not string like '->' [OK]
Common Mistakes:
  • Passing arrowprops as string instead of dict
  • Confusing axhspan with axvspan usage
  • Thinking xytext must be inside plot limits
5. You want to highlight the time period between 10 and 15 seconds on a line plot and annotate the highest point in that range with a label 'Max'. Which code snippet correctly achieves this?
hard
A. plt.axvspan(10, 15, color='lightblue', alpha=0.4) max_x = 12 max_y = 50 plt.annotate('Max', xy=(max_x, max_y), xytext=(max_x+1, max_y+5), arrowprops=dict(facecolor='red'))
B. plt.axhspan(10, 15, color='lightblue', alpha=0.4) max_x = 12 max_y = 50 plt.annotate('Max', xy=(max_x, max_y), xytext=(max_x-1, max_y-5), arrowprops='->')
C. plt.axvspan(10, 15, color='lightblue') max_x = 12 max_y = 50 plt.annotate('Max', xy=(max_x, max_y), xytext=(max_x, max_y), arrowprops=dict(color='green'))
D. plt.axvspan(10, 15, color='yellow', alpha=0.4) max_x = 12 max_y = 50 plt.annotate('Max', xy=(max_x, max_y), xytext=(max_x+2, max_y+2))

Solution

  1. Step 1: Highlight the time period on x-axis

    plt.axvspan(10, 15, color='lightblue', alpha=0.4) correctly highlights between 10 and 15 seconds with transparency.
  2. Step 2: Annotate the highest point with arrow

    Using plt.annotate with arrowprops=dict(facecolor='red') adds a red arrow pointing to (12, 50) with text offset at (13, 55).
  3. Final Answer:

    plt.axvspan(10, 15, color='lightblue', alpha=0.4) max_x = 12 max_y = 50 plt.annotate('Max', xy=(max_x, max_y), xytext=(max_x+1, max_y+5), arrowprops=dict(facecolor='red')) -> Option A
  4. Quick Check:

    axvspan for x-range, annotate with arrowprops dict [OK]
Hint: Use axvspan for x-range highlight and dict arrowprops for arrow [OK]
Common Mistakes:
  • Using axhspan instead of axvspan for time range
  • Passing arrowprops as string instead of dict
  • Not setting alpha for transparency in highlight