0
0
Matplotlibdata~20 mins

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

Choose your learning style9 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.