0
0
Matplotlibdata~10 mins

Text annotations 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 add a text annotation at coordinates (2, 3) on the plot.

Matplotlib
import matplotlib.pyplot as plt
plt.plot([1, 2, 3], [1, 4, 9])
plt.[1](2, 3, 'Point')
plt.show()
Drag options to blanks, or click blank then click option'
Atitle
Blabel
Ctext
Dannotate
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'annotate' instead of 'text' for simple text placement.
Trying to use 'label' which is not a matplotlib function.
Using 'title' which sets the plot title, not text at coordinates.
2fill in blank
medium

Complete the code to add an annotation with an arrow pointing to (2, 4) with text 'Max'.

Matplotlib
import matplotlib.pyplot as plt
plt.plot([1, 2, 3], [1, 4, 9])
plt.annotate('Max', xy=(2, 4), xytext=(3, 5), arrowprops=[1])
plt.show()
Drag options to blanks, or click blank then click option'
A{'arrowstyle': '->'}
B{'style': '->'}
C{'arrow': '->'}
D{'arrowstyle': '-->'}
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'style' instead of 'arrowstyle' as the key.
Using 'arrow' which is not a valid key.
Using '-->' which is not a recognized arrow style.
3fill in blank
hard

Fix the error in the code to correctly place an annotation with an arrow.

Matplotlib
import matplotlib.pyplot as plt
plt.plot([1, 2, 3], [1, 4, 9])
plt.annotate('Peak', xy=(3, 9), xytext=(2, 7), arrowprops=[1])
plt.show()
Drag options to blanks, or click blank then click option'
A{'arrowstyle': '->'}
B'arrowstyle': '->'
C{'arrowstyle': '->', 'color': 'red'}
D{'arrow': '->'}
Attempts:
3 left
💡 Hint
Common Mistakes
Passing a string instead of a dictionary for arrowprops.
Using an invalid key like 'arrow' instead of 'arrowstyle'.
Forgetting the braces around the dictionary.
4fill in blank
hard

Fill both blanks to create a dictionary comprehension that maps words to their lengths only if length is greater than 3.

Matplotlib
words = ['data', 'science', 'ai', 'ml']
lengths = {word: [1] for word in words if [2]
Drag options to blanks, or click blank then click option'
Alen(word)
Blen(word) > 3
Cword
Dword > 3
Attempts:
3 left
💡 Hint
Common Mistakes
Using the word itself as the value instead of its length.
Using the word in the condition instead of its length.
Using incorrect comparison operators.
5fill in blank
hard

Fill all three blanks to create a dictionary comprehension that maps uppercase words to their lengths if length is greater than 2.

Matplotlib
words = ['cat', 'dog', 'a', 'bird']
result = { [1]: [2] for w in words if [3] }
Drag options to blanks, or click blank then click option'
Aw.upper()
Blen(w)
Clen(w) > 2
Dw
Attempts:
3 left
💡 Hint
Common Mistakes
Using the original word as key instead of uppercase.
Using the word itself as value instead of length.
Incorrect condition for filtering.