Bird
Raised Fist0
Matplotlibdata~20 mins

Multi-line text in Matplotlib - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Multi-line Text 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 multi-line text plot code?

Consider the following Python code using matplotlib. What will be the text displayed on the plot?

Matplotlib
import matplotlib.pyplot as plt

fig, ax = plt.subplots()
text = "Line 1\nLine 2\nLine 3"
ax.text(0.5, 0.5, text, ha='center', va='center')
plt.show()
AThe plot shows three lines of text stacked vertically: 'Line 1', 'Line 2', and 'Line 3' centered on the plot.
BThe plot shows the text 'Line 1\nLine 2\nLine 3' as a single line with literal \n characters visible.
CThe plot is empty because the text is not displayed without specifying a font size.
DThe plot shows only 'Line 1' because matplotlib ignores newlines in text.
Attempts:
2 left
💡 Hint

Think about how matplotlib handles newline characters in text strings.

🔧 Debug
intermediate
2:00remaining
Why does this multi-line text not display correctly?

Look at this code snippet:

import matplotlib.pyplot as plt

fig, ax = plt.subplots()
text = 'First line\nSecond line\nThird line'
ax.text(0.5, 0.5, text, ha='center', va='center', wrap=True)
plt.show()

Why might the text not wrap or display as expected?

ABecause the <code>wrap</code> parameter is not supported by <code>ax.text</code>, so it is ignored.
BBecause the text string uses single quotes instead of double quotes, causing an error.
CBecause the coordinates (0.5, 0.5) are outside the plot area, so text is not visible.
DBecause the <code>ha</code> and <code>va</code> parameters must be set to 'left' and 'top' for multi-line text.
Attempts:
2 left
💡 Hint

Check the official documentation for ax.text parameters.

❓ visualization
advanced
2:00remaining
Which option produces a plot with multi-line text using plt.text with custom line spacing?

Given the goal to display multi-line text with extra space between lines, which code snippet achieves this?

A
plt.text(0.5, 0.5, 'Line 1\nLine 2\nLine 3', ha='center', va='center', lineheight=2)
plt.show()
B
plt.text(0.5, 0.5, 'Line 1\nLine 2\nLine 3', ha='center', va='center', line_spacing=2)
plt.show()
C
plt.text(0.5, 0.5, 'Line 1\nLine 2\nLine 3', ha='center', va='center', spacing=2)
plt.show()
D
plt.text(0.5, 0.5, 'Line 1\nLine 2\nLine 3', ha='center', va='center', linespacing=2)
plt.show()
Attempts:
2 left
💡 Hint

Check the exact parameter name for line spacing in plt.text.

🧠 Conceptual
advanced
2:00remaining
How does matplotlib handle multi-line text alignment?

When using multi-line text in matplotlib, how does the ha (horizontal alignment) parameter affect the text?

A<p><code>ha</code> aligns the entire block of multi-line text as a single unit relative to the x coordinate.</p>
B<p><code>ha</code> aligns each line of the multi-line text relative to the specified x coordinate.</p>
C<p><code>ha</code> only affects the first line of the multi-line text.</p>
D<p><code>ha</code> has no effect on multi-line text alignment.</p>
Attempts:
2 left
💡 Hint

Think about how each line is positioned horizontally.

❓ data_output
expert
3:00remaining
What is the bounding box size of this multi-line text?

Given this code, what is the approximate bounding box width and height of the multi-line text in display units?

Matplotlib
import matplotlib.pyplot as plt

fig, ax = plt.subplots()
text_obj = ax.text(0.5, 0.5, 'Hello\nWorld', ha='center', va='center', fontsize=12)
fig.canvas.draw()
bbox = text_obj.get_window_extent()
width, height = bbox.width, bbox.height
print(round(width), round(height))
A"The width and height are both about 45"
B"The width is about 30 and the height is about 60"
C"The width is about 60 and the height is about 30"
D"The width and height are both about 15"
Attempts:
2 left
💡 Hint

Remember that width is usually larger than height for horizontal text, and multi-line text height increases with lines.

Practice

(1/5)
1. What does the \n character do inside a text string in matplotlib?
easy
A. It adds a space between words.
B. It changes the text color.
C. It creates a new line, splitting the text into multiple lines.
D. It makes the text bold.

Solution

  1. Step 1: Understand the role of \n in strings

    The \n character is a newline character that breaks text into multiple lines.
  2. Step 2: Apply this to matplotlib text

    Using \n inside a string in matplotlib text splits the text into separate lines on the plot.
  3. Final Answer:

    It creates a new line, splitting the text into multiple lines. -> Option C
  4. Quick Check:

    Newline character = creates new line [OK]
Hint: Remember \n means new line inside text strings [OK]
Common Mistakes:
  • Thinking \n adds space instead of new line
  • Confusing \n with color or style commands
  • Using \n outside text strings
2. Which of the following is the correct way to add multi-line text at position (2, 3) in a matplotlib plot?
easy
A. plt.text(2, 3, 'Line1\nLine2')
B. plt.text('2, 3', 'Line1\nLine2')
C. plt.text(2, 3, 'Line1\Line2')
D. plt.text(2, 3, 'Line1/Line2')

Solution

  1. Step 1: Check the syntax of plt.text()

    The function plt.text(x, y, text) requires numeric x and y coordinates and a string for text.
  2. Step 2: Identify the newline character

    The newline character is \n, so 'Line1\nLine2' correctly creates two lines.
  3. Final Answer:

    plt.text(2, 3, 'Line1\nLine2') -> Option A
  4. Quick Check:

    Correct syntax with \n = plt.text(2, 3, 'Line1\nLine2') [OK]
Hint: Use plt.text(x, y, 'line1\nline2') for multi-line text [OK]
Common Mistakes:
  • Passing coordinates as a string instead of numbers
  • Using wrong escape characters like \ or /
  • Missing the newline character \n
3. What will the following code display on the plot?
import matplotlib.pyplot as plt
plt.plot([1, 2, 3], [4, 5, 6])
plt.text(2, 5, 'Hello\nWorld', fontsize=12, color='red')
plt.show()
medium
A. A plot with red text 'Hello' on one line and 'World' below it at position (2,5).
B. A plot with red text 'Hello World' on a single line at position (2,5).
C. A plot with blue text 'Hello\nWorld' at position (2,5).
D. A plot with no text displayed.

Solution

  1. Step 1: Understand plt.text parameters

    The text is placed at (2,5) with font size 12 and color red. The string contains a newline \n.
  2. Step 2: Interpret the newline in text

    The \n splits 'Hello' and 'World' into two lines, so text appears as two lines stacked vertically.
  3. Final Answer:

    A plot with red text 'Hello' on one line and 'World' below it at position (2,5). -> Option A
  4. Quick Check:

    \n splits lines, color='red' applies [OK]
Hint: Look for \n to split lines and color param for text color [OK]
Common Mistakes:
  • Ignoring the newline and expecting one line
  • Confusing color red with blue
  • Thinking text won't appear without plt.show()
4. Identify the error in this code snippet that tries to add multi-line text:
plt.text(1, 2, 'Line1\Line2', fontsize=10)
medium
A. The text string is missing quotes.
B. The fontsize parameter is invalid.
C. The coordinates (1, 2) are out of plot range.
D. The newline character is incorrect; it should be '\n' not '\'.

Solution

  1. Step 1: Check the newline character usage

    The code uses '\' instead of '\n' to create a new line, which is incorrect.
  2. Step 2: Confirm other parameters

    Fontsize is valid, coordinates are numeric, and text is quoted properly.
  3. Final Answer:

    The newline character is incorrect; it should be '\n' not '\'. -> Option D
  4. Quick Check:

    Use \n for new lines, not single backslash [OK]
Hint: Newline must be \n, not just \ alone [OK]
Common Mistakes:
  • Using single backslash instead of \n
  • Assuming fontsize causes error
  • Thinking coordinates must be strings
5. You want to add a multi-line label on a plot at (0.5, 0.5) with the text:
"Temperature:\nHigh: 30°C\nLow: 15°C"
and make sure the text is centered and blue. Which code snippet achieves this?
hard
A. plt.text(0.5, 0.5, 'Temperature:\nHigh: 30°C\nLow: 15°C', fontsize=14, ha='center')
B. plt.text(0.5, 0.5, 'Temperature:\nHigh: 30°C\nLow: 15°C', color='blue', fontsize=14, ha='center')
C. plt.text(0.5, 0.5, 'Temperature\nHigh: 30°C\nLow: 15°C', color='red', fontsize=14, ha='left')
D. plt.text(0.5, 0.5, 'Temperature\nHigh: 30°C\nLow: 15°C', color='blue', fontsize=10, ha='right')

Solution

  1. Step 1: Check the text string for new lines

    The text uses '\n' correctly to create multiple lines.
  2. Step 2: Verify color, font size, and alignment

    Color is blue, fontsize is 14, and horizontal alignment (ha) is 'center' as required.
  3. Final Answer:

    plt.text(0.5, 0.5, 'Temperature:\nHigh: 30°C\nLow: 15°C', color='blue', fontsize=14, ha='center') -> Option B
  4. Quick Check:

    Correct text, color blue, center align = plt.text(0.5, 0.5, 'Temperature:\nHigh: 30°C\nLow: 15°C', color='blue', fontsize=14, ha='center') [OK]
Hint: Use ha='center' and color='blue' for centered blue multi-line text [OK]
Common Mistakes:
  • Using wrong color or alignment
  • Missing \n for new lines
  • Setting fontsize too small or default
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