0
0
Matplotlibdata~10 mins

Text alignment options 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 set the horizontal alignment of the text to center.

Matplotlib
import matplotlib.pyplot as plt
plt.text(0.5, 0.5, 'Hello', ha=[1])
plt.show()
Drag options to blanks, or click blank then click option'
A'right'
B'left'
C'justify'
D'center'
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'left' or 'right' instead of 'center' for centering text.
2fill in blank
medium

Complete the code to set the vertical alignment of the text to top.

Matplotlib
import matplotlib.pyplot as plt
plt.text(0.5, 0.5, 'Top aligned', va=[1])
plt.show()
Drag options to blanks, or click blank then click option'
A'top'
B'center'
C'bottom'
D'baseline'
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'bottom' or 'center' instead of 'top' for vertical alignment.
3fill in blank
hard

Fix the error in the code by choosing the correct horizontal alignment value.

Matplotlib
import matplotlib.pyplot as plt
plt.text(0.5, 0.5, 'Error fix', ha=[1])
plt.show()
Drag options to blanks, or click blank then click option'
A'middle'
B'centre'
C'center'
D'centre-align'
Attempts:
3 left
💡 Hint
Common Mistakes
Using British spelling 'centre' instead of 'center'.
Using non-existent values like 'middle' or 'centre-align'.
4fill in blank
hard

Fill both blanks to center the text horizontally and align it vertically at the baseline.

Matplotlib
import matplotlib.pyplot as plt
plt.text(0.5, 0.5, 'Aligned Text', ha=[1], va=[2])
plt.show()
Drag options to blanks, or click blank then click option'
A'center'
B'baseline'
C'top'
D'left'
Attempts:
3 left
💡 Hint
Common Mistakes
Mixing up horizontal and vertical alignment values.
Using 'top' instead of 'baseline' for vertical alignment.
5fill in blank
hard

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

Matplotlib
words = ['data', 'science', 'ai', 'ml']
lengths = { [1] : [2] for [3] in words if len([3]) > 3 }
Drag options to blanks, or click blank then click option'
Aword
Blen(word)
Ditem
Attempts:
3 left
💡 Hint
Common Mistakes
Using different variable names inconsistently.
Using the word variable as value instead of length.