0
0
Matplotlibdata~20 mins

Mathematical expressions with LaTeX in Matplotlib - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
LaTeX Plot Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
Output of LaTeX formatted plot title
What will be the title text shown on the plot after running this code?
Matplotlib
import matplotlib.pyplot as plt
plt.plot([1, 2, 3], [1, 4, 9])
plt.title(r'$y = x^2$')
plt.show()
AThe plot title shows the text: y = x^2 with superscript formatting
BThe plot title shows the text: y = x2 without superscript
CThe plot title is empty with no text shown
DThe plot title shows the raw string: $y = x^2$ without formatting
Attempts:
2 left
💡 Hint
Look at how LaTeX math expressions are wrapped with $ signs inside the raw string.
data_output
intermediate
2:00remaining
Number of tick labels with LaTeX formatting
How many x-axis tick labels will be shown after running this code?
Matplotlib
import matplotlib.pyplot as plt
import numpy as np
x = np.linspace(0, 2, 5)
plt.plot(x, x**2)
plt.xticks(x, [f'$x_{{{int(i)}}}$' for i in x])
plt.show()
A5 tick labels with LaTeX formatted subscripts x_0, x_0.5, x_1, x_1.5, x_2
B5 tick labels with LaTeX formatted subscripts x_0, x_0, x_1, x_1, x_2
C5 tick labels with LaTeX formatted subscripts x_0, x_1, x_2, x_3, x_4
D5 tick labels with LaTeX formatted subscripts x_0, x_1, x_2, x_3, x_5
Attempts:
2 left
💡 Hint
Look carefully at how the labels are created using int(i) for each tick value.
visualization
advanced
2:00remaining
Correct LaTeX rendering of integral expression in plot
Which option produces a plot with the title showing the integral \( \int_0^1 x^2 dx \) correctly formatted?
Aplt.title(r'\int_0^1 x^2 dx')
Bplt.title('$\int_0^1 x^2 dx$')
Cplt.title('\int_0^1 x^2 dx')
Dplt.title(r'$\int_0^1 x^2 dx$')
Attempts:
2 left
💡 Hint
Raw strings (r'') help avoid needing to double escape backslashes in LaTeX.
🔧 Debug
advanced
2:00remaining
Identify the error in LaTeX expression for plot label
What error will this code produce when run?
Matplotlib
import matplotlib.pyplot as plt
plt.plot([1, 2, 3], [1, 4, 9])
plt.xlabel(r'$\frac{1}{2$')
plt.show()
ARuntimeError from matplotlib about invalid LaTeX syntax
BSyntaxError due to unmatched brace in LaTeX expression
CThe label shows raw text $\frac{1}{2$ without formatting
DNo error, label shows fraction 1/2 correctly
Attempts:
2 left
💡 Hint
Check if all braces in the LaTeX expression are properly closed.
🚀 Application
expert
3:00remaining
Create a plot with multiple LaTeX formatted annotations
Which code snippet correctly adds two LaTeX formatted annotations at points (1,1) and (2,4) on the plot?
Aplt.annotate(r'y=1', (1,1)); plt.annotate(r'y=2^2', (2,4))
Bplt.annotate('$y=1$', (1,1)); plt.annotate('$y=2^2$', (2,4))
Cplt.annotate(r'$y=1$', (1,1)); plt.annotate(r'$y=2^2$', (2,4))
Dplt.annotate('y=1', (1,1)); plt.annotate('y=2^2', (2,4))
Attempts:
2 left
💡 Hint
Raw strings with $ delimiters are needed for LaTeX math rendering in annotations.