Bird
Raised Fist0
Matplotlibdata~20 mins

Text alignment options in Matplotlib - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Text Alignment Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate
2: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()
AThe text is aligned to the right of the specified position.
BThe text is centered horizontally on the specified position.
CThe text is aligned to the left of the specified position.
DThe text alignment is default and depends on the matplotlib version.
Attempts:
2 left
💡 Hint
The parameter 'ha' controls horizontal alignment: 'left', 'center', or 'right'.
❓ Predict Output
intermediate
2: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()
AThe text alignment is baseline by default.
BThe text is aligned so its top is at the y position.
CThe text is aligned so its bottom is at the y position.
DThe text is aligned so its center is at the y position.
Attempts:
2 left
💡 Hint
The 'va' parameter controls vertical alignment: 'top', 'center', 'bottom', or 'baseline'.
❓ visualization
advanced
2: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()
AThe text at x=0.2, y=0.5
BThe text at x=0.8, y=0.5
CNone of the texts are centered both horizontally and vertically.
DThe text at x=0.5, y=0.5
Attempts:
2 left
💡 Hint
Look for ha='center' and va='center' in the code.
❓ data_output
advanced
2: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)
A3
B1
C2
D4
Attempts:
2 left
💡 Hint
Count how many text objects have ha='right'.
🧠 Conceptual
expert
2: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?
Aha='center' and va='center'
Bha='left' and va='bottom'
Cha='right' and va='top'
Dha='center' and va='baseline'
Attempts:
2 left
💡 Hint
Centering means the text's middle aligns with the point horizontally and vertically.

Practice

(1/5)
1. In matplotlib, which parameter controls the horizontal alignment of text relative to its (x, y) position?
easy
A. va
B. ha
C. align
D. position

Solution

  1. Step 1: Understand text alignment parameters

    Matplotlib uses 'ha' for horizontal alignment and 'va' for vertical alignment.
  2. Step 2: Identify horizontal alignment parameter

    The parameter 'ha' stands for horizontal alignment, controlling left, center, or right placement.
  3. Final Answer:

    ha -> Option B
  4. Quick Check:

    Horizontal alignment = ha [OK]
Hint: Remember 'h' in 'ha' means horizontal [OK]
Common Mistakes:
  • Confusing 'ha' with 'va'
  • Using 'align' which is not a valid parameter
  • Assuming 'position' controls alignment
2. Which of the following is the correct way to set vertical alignment to 'top' in matplotlib's text() function?
easy
A. plt.text(x, y, 'label', va='top')
B. plt.text(x, y, 'label', ha='top')
C. plt.text(x, y, 'label', valign='top')
D. plt.text(x, y, 'label', vertical='top')

Solution

  1. Step 1: Recall vertical alignment parameter

    Vertical alignment is controlled by 'va' in matplotlib's text function.
  2. Step 2: Check correct syntax for setting 'top'

    Using va='top' correctly sets vertical alignment to top.
  3. Final Answer:

    plt.text(x, y, 'label', va='top') -> Option A
  4. Quick Check:

    Vertical alignment = va [OK]
Hint: Use 'va' for vertical alignment, not 'ha' or others [OK]
Common Mistakes:
  • Using 'ha' instead of 'va' for vertical alignment
  • Using non-existent parameters like 'valign' or 'vertical'
  • Misspelling parameter names
3. What will be the horizontal alignment of the text in this code snippet?
import matplotlib.pyplot as plt
plt.text(0.5, 0.5, 'Center', ha='center', va='bottom')
plt.show()
medium
A. Center aligned
B. Right aligned
C. Left aligned
D. Justified

Solution

  1. Step 1: Analyze the 'ha' parameter in the code

    The code sets ha='center', which means horizontal alignment is centered on the x position.
  2. Step 2: Understand the effect of ha='center'

    The text will be horizontally centered at x=0.5 on the plot.
  3. Final Answer:

    Center aligned -> Option A
  4. Quick Check:

    ha='center' means center aligned [OK]
Hint: ha='center' means text is centered horizontally [OK]
Common Mistakes:
  • Confusing 'ha' with 'va' and expecting vertical alignment
  • Assuming default alignment is left
  • Thinking 'ha'='center' means justified
4. Identify the error in this matplotlib code snippet:
import matplotlib.pyplot as plt
plt.text(0.3, 0.7, 'Hello', ha='middle', va='center')
plt.show()
medium
A. va parameter should be 'middle' instead of 'center'
B. Missing plt.show() call
C. Incorrect order of parameters
D. Invalid value 'middle' for ha parameter

Solution

  1. Step 1: Check valid values for 'ha'

    Valid 'ha' values are 'left', 'center', and 'right'. 'middle' is invalid.
  2. Step 2: Verify other parameters and syntax

    va='center' is valid, plt.show() is present, and parameter order does not matter.
  3. Final Answer:

    Invalid value 'middle' for ha parameter -> Option D
  4. Quick Check:

    ha must be left, center, or right [OK]
Hint: Use only 'left', 'center', or 'right' for ha [OK]
Common Mistakes:
  • Using 'middle' instead of 'center' for ha
  • Thinking parameter order matters
  • Ignoring error messages about invalid parameter values
5. You want to place a label exactly at the top-right corner of a plot using plt.text(). Which combination of ha and va will correctly align the text so that the label's top-right corner is at the given (x, y) coordinates?
hard
A. ha='left', va='bottom'
B. ha='center', va='center'
C. ha='right', va='top'
D. ha='right', va='bottom'

Solution

  1. Step 1: Understand alignment relative to (x, y)

    ha='right' aligns the text's right edge to x, va='top' aligns the text's top edge to y.
  2. Step 2: Match alignment to top-right corner placement

    To place text so its top-right corner is at (x, y), use ha='right' and va='top'.
  3. Final Answer:

    ha='right', va='top' -> Option C
  4. Quick Check:

    Right and top align text corner to (x, y) [OK]
Hint: Right + top aligns text's top-right corner [OK]
Common Mistakes:
  • Using ha='left' or va='bottom' which aligns opposite corners
  • Assuming center alignment places corner at (x, y)
  • Mixing up horizontal and vertical alignment parameters
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard