Challenge - 5 Problems
Text Alignment Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
What is the horizontal alignment of the text?
Consider the following matplotlib code snippet. What is the horizontal alignment of the text 'Hello' on the plot?
Matplotlib
import matplotlib.pyplot as plt plt.text(0.5, 0.5, 'Hello', ha='right') plt.close()
Attempts:
2 left
💡 Hint
The parameter 'ha' controls horizontal alignment: 'left', 'center', or 'right'.
✗ Incorrect
The 'ha' parameter set to 'right' means the text's right edge is at the specified x coordinate.
❓ Predict Output
intermediate2:00remaining
What vertical alignment does this code produce?
Given this matplotlib code, what vertical alignment is applied to the text 'Data'?
Matplotlib
import matplotlib.pyplot as plt plt.text(0.5, 0.5, 'Data', va='top') plt.close()
Attempts:
2 left
💡 Hint
The 'va' parameter controls vertical alignment: 'top', 'center', 'bottom', or 'baseline'.
✗ Incorrect
Setting va='top' aligns the top of the text at the given y coordinate.
❓ visualization
advanced2:30remaining
Identify the text alignment from the plot
This plot shows three texts with different horizontal and vertical alignments. Which text is centered both horizontally and vertically?
Matplotlib
import matplotlib.pyplot as plt fig, ax = plt.subplots() ax.text(0.2, 0.5, 'Left-Bottom', ha='left', va='bottom') ax.text(0.5, 0.5, 'Center-Center', ha='center', va='center') ax.text(0.8, 0.5, 'Right-Top', ha='right', va='top') plt.close()
Attempts:
2 left
💡 Hint
Look for ha='center' and va='center' in the code.
✗ Incorrect
The text at (0.5, 0.5) uses ha='center' and va='center', so it is centered horizontally and vertically.
❓ data_output
advanced2:00remaining
Count texts with right horizontal alignment
Given this list of matplotlib text objects with different horizontal alignments, how many have 'ha' set to 'right'?
Matplotlib
import matplotlib.text as mtext texts = [mtext.Text(0,0,'A', ha='left'), mtext.Text(0,0,'B', ha='right'), mtext.Text(0,0,'C', ha='center'), mtext.Text(0,0,'D', ha='right')] count_right = sum(1 for t in texts if t.get_ha() == 'right') print(count_right)
Attempts:
2 left
💡 Hint
Count how many text objects have ha='right'.
✗ Incorrect
Two text objects have ha='right': 'B' and 'D'.
🧠 Conceptual
expert2:00remaining
Which alignment combination places text exactly centered on a point?
In matplotlib, to place text exactly centered on a point (x, y), which combination of horizontal and vertical alignment should be used?
Attempts:
2 left
💡 Hint
Centering means the text's middle aligns with the point horizontally and vertically.
✗ Incorrect
Using ha='center' and va='center' aligns the text's center exactly at the point.