Bird
Raised Fist0
Matplotlibdata~20 mins

Text placement with ax.text in Matplotlib - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Text Placement Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate
2:00remaining
What is the output of this matplotlib text placement code?

Consider the following code that places text on a plot using ax.text. What text will appear at coordinates (0.5, 0.5)?

Matplotlib
import matplotlib.pyplot as plt
fig, ax = plt.subplots()
ax.text(0.5, 0.5, 'Center', ha='center', va='center')
plt.close(fig)
print(ax.texts[0].get_text())
A"ha='center', va='center'"
B"Center"
C"0.5, 0.5"
D"center"
Attempts:
2 left
💡 Hint

Look at the third argument of ax.text. It is the string that will be displayed.

❓ data_output
intermediate
2:00remaining
How many text objects are on the axes after this code?

After running the code below, how many text objects are present on the axes?

Matplotlib
import matplotlib.pyplot as plt
fig, ax = plt.subplots()
ax.text(0.1, 0.1, 'A')
ax.text(0.2, 0.2, 'B')
ax.text(0.3, 0.3, 'C')
plt.close(fig)
print(len(ax.texts))
A3
B2
C1
D0
Attempts:
2 left
💡 Hint

Each call to ax.text adds one text object to the axes.

❓ visualization
advanced
2:00remaining
Which option shows the correct vertical alignment of text?

Given the code below, which option correctly describes the vertical alignment of the text?

import matplotlib.pyplot as plt
fig, ax = plt.subplots()
ax.text(0.5, 0.5, 'Text', va='top')
plt.close(fig)
AThe text is aligned so its top is at y=0.5
BThe text is aligned so its bottom is at y=0.5
CThe text is aligned so its center is at y=0.5
DThe text is aligned so its baseline is at y=0.5
Attempts:
2 left
💡 Hint

Vertical alignment va='top' means the top of the text is at the given y coordinate.

🧠 Conceptual
advanced
2:00remaining
What happens if you set ha='right' in ax.text?

In matplotlib, what does setting ha='right' do to the text placement?

AThe center of the text is placed at the x coordinate
BThe left edge of the text is placed at the x coordinate
CThe right edge of the text is placed at the x coordinate
DThe text is rotated 90 degrees
Attempts:
2 left
💡 Hint

Horizontal alignment ha='right' aligns the right edge of the text to the x coordinate.

🔧 Debug
expert
2:00remaining
What error does this code raise?

What error will this code raise?

import matplotlib.pyplot as plt
fig, ax = plt.subplots()
ax.text(0.5, 0.5, 'Hello', ha='middle')
plt.close(fig)
ANo error, text is placed with default alignment
BTypeError: text() got an unexpected keyword argument 'ha'
CSyntaxError: invalid syntax
DValueError: Invalid horizontal alignment
Attempts:
2 left
💡 Hint

Check the valid values for ha in matplotlib documentation.

Practice

(1/5)
1. What does the ax.text function do in matplotlib?
easy
A. It draws a line between two points.
B. It places text at specified x and y coordinates on the plot.
C. It creates a new figure window.
D. It changes the background color of the plot.

Solution

  1. Step 1: Understand the purpose of ax.text

    The ax.text function is used to add text labels at specific positions on a plot using x and y coordinates.
  2. Step 2: Compare with other options

    Other options describe different matplotlib functions: creating figures, drawing lines, or changing background color, which are not what ax.text does.
  3. Final Answer:

    It places text at specified x and y coordinates on the plot. -> Option B
  4. Quick Check:

    ax.text adds text labels = C [OK]
Hint: ax.text adds text at (x, y) on the plot [OK]
Common Mistakes:
  • Confusing ax.text with plot or figure functions
  • Thinking ax.text changes plot colors
  • Assuming ax.text draws shapes
2. Which of the following is the correct syntax to place the text 'Hello' at coordinates (2, 3) using ax.text?
easy
A. ax.text(2, 3, 'Hello')
B. ax.text('Hello', 2, 3)
C. ax.text(x=3, y=2, text='Hello')
D. ax.text(3, 2, 'Hello')

Solution

  1. Step 1: Recall ax.text parameter order

    The correct order is ax.text(x, y, text), so x=2, y=3, text='Hello' means ax.text(2, 3, 'Hello').
  2. Step 2: Check other options

    ax.text('Hello', 2, 3) swaps text and coordinates, ax.text(x=3, y=2, text='Hello') uses wrong parameter names, ax.text(3, 2, 'Hello') swaps x and y values.
  3. Final Answer:

    ax.text(2, 3, 'Hello') -> Option A
  4. Quick Check:

    ax.text(x, y, text) = A [OK]
Hint: Remember ax.text(x, y, text) order [OK]
Common Mistakes:
  • Swapping x and y coordinates
  • Putting text before coordinates
  • Using wrong parameter names
3. What will the following code display on the plot?
import matplotlib.pyplot as plt
fig, ax = plt.subplots()
ax.plot([1, 2, 3], [4, 5, 6])
ax.text(2, 5, 'Center', fontsize=12, color='red')
plt.show()
medium
A. An error because color must be uppercase.
B. A blue text 'Center' at point (2, 5) on the plot.
C. No text will appear because fontsize is missing.
D. A red text 'Center' at point (2, 5) on the plot.

Solution

  1. Step 1: Analyze ax.text parameters

    The code places text 'Center' at (2, 5) with font size 12 and color red, which is valid syntax.
  2. Step 2: Understand matplotlib behavior

    Text appears on the plot at the given coordinates with specified style. Color names are case-insensitive, so 'red' works fine.
  3. Final Answer:

    A red text 'Center' at point (2, 5) on the plot. -> Option D
  4. Quick Check:

    ax.text with color='red' shows red text = A [OK]
Hint: Color names in ax.text are case-insensitive [OK]
Common Mistakes:
  • Thinking fontsize is mandatory to show text
  • Assuming color must be uppercase
  • Expecting text at wrong coordinates
4. Identify the error in this code snippet:
fig, ax = plt.subplots()
ax.text(1, 2, fontsize='large')
plt.show()
medium
A. plt.show() must come before ax.text.
B. ax.text requires x and y as strings, not numbers.
C. The text argument is missing.
D. fontsize must be an integer or float, not a string like 'large'.

Solution

  1. Step 1: Check ax.text parameters

    Matplotlib's ax.text requires three positional arguments: x, y, and text (s). The code provides only x=1, y=2, then a keyword argument, missing the text.
  2. Step 2: Verify other options

    Numbers for x/y are correct, fontsize='large' is valid, and plt.show() order is fine.
  3. Final Answer:

    The text argument is missing. -> Option C
  4. Quick Check:

    ax.text requires x, y, text = C [OK]
Hint: ax.text requires x, y, text positional args [OK]
Common Mistakes:
  • Omitting the text argument
  • Confusing parameter order
  • Calling plt.show() too early
5. You want to add a label 'Max Point' exactly above the highest point in this plot:
import matplotlib.pyplot as plt
x = [1, 2, 3, 4]
y = [2, 5, 1, 7]
fig, ax = plt.subplots()
ax.plot(x, y, marker='o')
# Add text here
plt.show()

Which code correctly places the label 0.3 units above the highest point?
hard
A. ax.text(4, 7.3, 'Max Point', va='bottom', ha='center')
B. ax.text(4, 7.3, 'Max Point', ha='center')
C. ax.text(4, 7, 'Max Point', va='top')
D. ax.text(4, 7, 'Max Point', ha='left')

Solution

  1. Step 1: Identify highest point coordinates

    The highest y value is 7 at x=4.
  2. Step 2: Place text 0.3 units above with correct alignment

    To place text above, y=7.3 is correct. Use vertical alignment va='bottom' so text bottom aligns at 7.3, placing text above the point. Horizontal alignment ha='center' centers text horizontally at x=4.
  3. Final Answer:

    ax.text(4, 7.3, 'Max Point', va='bottom', ha='center') -> Option A
  4. Quick Check:

    Text above point uses y+offset and va='bottom' = B [OK]
Hint: Use va='bottom' to place text above y coordinate [OK]
Common Mistakes:
  • Using va='top' places text below the coordinate
  • Not adding offset to y coordinate
  • Misaligning text horizontally
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