0
0
Matplotlibdata~10 mins

Text placement with ax.text 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 text at position (2, 3) on the plot.

Matplotlib
import matplotlib.pyplot as plt
fig, ax = plt.subplots()
ax.plot([1, 2, 3], [1, 4, 9])
ax.text(2, 3, [1])
plt.show()
Drag options to blanks, or click blank then click option'
A"Hello"
BHello
C2, 3
Dax.plot
Attempts:
3 left
💡 Hint
Common Mistakes
Forgetting to put the text inside quotes.
Passing numbers instead of a string as the text.
2fill in blank
medium

Complete the code to place the text 'Peak' at the highest point on the plot.

Matplotlib
import matplotlib.pyplot as plt
fig, ax = plt.subplots()
x = [1, 2, 3]
y = [1, 4, 9]
ax.plot(x, y)
ax.text(x[[1]], y[[1]], "Peak")
plt.show()
Drag options to blanks, or click blank then click option'
A0
B3
C1
D2
Attempts:
3 left
💡 Hint
Common Mistakes
Using index 3 which is out of range.
Using index 1 which is not the highest point.
3fill in blank
hard

Fix the error in the code to correctly place the text 'Center' at (0.5, 0.5).

Matplotlib
import matplotlib.pyplot as plt
fig, ax = plt.subplots()
ax.text([1], 0.5, 'Center')
plt.show()
Drag options to blanks, or click blank then click option'
A'0.5'
Bcenter
C0.5
D0,5
Attempts:
3 left
💡 Hint
Common Mistakes
Using a string instead of a number for the coordinate.
Using a comma instead of a dot for decimal.
4fill in blank
hard

Fill both blanks to add text 'Max' at the maximum y value and align it to the right.

Matplotlib
import matplotlib.pyplot as plt
fig, ax = plt.subplots()
x = [1, 2, 3, 4]
y = [2, 5, 1, 7]
ax.plot(x, y)
max_index = y.index(max(y))
ax.text(x[[1]], y[[2]], 'Max', ha='right')
plt.show()
Drag options to blanks, or click blank then click option'
Amax_index
B0
D3
Attempts:
3 left
💡 Hint
Common Mistakes
Using different indexes for x and y coordinates.
Using a fixed number instead of max_index.
5fill in blank
hard

Fill all three blanks to create a dictionary with words as keys and their lengths as values, but only for words longer than 3 letters.

Matplotlib
words = ['cat', 'elephant', 'dog', 'horse']
lengths = { [1]: [2] for [1] in words if len([3]) > 3 }
Drag options to blanks, or click blank then click option'
Aword
Blen(word)
Dwords
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'words' instead of 'word' in the comprehension.
Not using len(word) for the value.
Using different variable names inconsistently.