0
0
Matplotlibdata~20 mins

LaTeX integration for papers 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
Matplotlib plot with LaTeX formatted title
What is the output of this code snippet that uses LaTeX in the plot title?
Matplotlib
import matplotlib.pyplot as plt
plt.plot([1, 2, 3], [1, 4, 9])
plt.title(r'$y = x^2$')
plt.savefig('plot.png')
plt.close()
print('Plot saved with LaTeX title')
AThe plot is saved but the title shows the raw string '$y = x^2$' without formatting
BThe plot is saved with the title showing y = x squared formatted using LaTeX math style
CThe code raises a SyntaxError due to incorrect LaTeX syntax in the title
DThe plot is saved but the title is empty because LaTeX is not supported
Attempts:
2 left
💡 Hint
Look at how raw strings and LaTeX math mode are used in matplotlib titles.
data_output
intermediate
2:00remaining
Effect of rcParams on LaTeX font in matplotlib
Given this code, what font family will be used in the plot labels?
Matplotlib
import matplotlib.pyplot as plt
plt.rcParams.update({
    'text.usetex': True,
    'font.family': 'serif',
    'font.serif': ['Palatino'],
})
plt.plot([0, 1], [0, 1])
plt.xlabel(r'$x$ axis')
plt.ylabel(r'$y$ axis')
plt.savefig('latex_font_plot.png')
plt.close()
print(plt.rcParams['font.family'])
A['sans-serif']
B['monospace']
C['Palatino']
D['serif']
Attempts:
2 left
💡 Hint
Check the value of plt.rcParams['font.family'] after update.
visualization
advanced
3:00remaining
Rendering complex LaTeX in matplotlib legend
Which option produces a plot with a legend showing the formula $\alpha = \frac{1}{\sqrt{2\pi\sigma^2}} e^{-\frac{(x-\mu)^2}{2\sigma^2}}$ correctly rendered?
Matplotlib
import matplotlib.pyplot as plt
import numpy as np
x = np.linspace(-3, 3, 100)
y = np.exp(-x**2 / 2) / np.sqrt(2 * np.pi)
plt.plot(x, y, label='')
plt.legend()
plt.show()
Aplt.plot(x, y, label=r'$\alpha = \frac{1}{\sqrt{2\pi\sigma^2}} e^{-\frac{(x-\mu)^2}{2\sigma^2}}$')
Bplt.plot(x, y, label='$\alpha = \frac{1}{\sqrt{2\pi\sigma^2}} e^{-\frac{(x-\mu)^2}{2\sigma^2}}$')
Cplt.plot(x, y, label='\alpha = \frac{1}{\sqrt{2\pi\sigma^2}} e^{-\frac{(x-\mu)^2}{2\sigma^2}}')
Dplt.plot(x, y, label=r'\alpha = \frac{1}{\sqrt{2\pi\sigma^2}} e^{-\frac{(x-\mu)^2}{2\sigma^2}}')
Attempts:
2 left
💡 Hint
Raw strings (r'') help avoid escape sequence errors in LaTeX strings.
🔧 Debug
advanced
2:00remaining
Identify the error in LaTeX integration with matplotlib
What error does this code raise when run?
Matplotlib
import matplotlib.pyplot as plt
plt.rcParams['text.usetex'] = True
plt.title('$E = mc^2$')
plt.plot([1, 2, 3], [1, 4, 9])
plt.show()
ATypeError: unsupported operand type(s) for ^: 'str' and 'int'
BSyntaxError: invalid syntax in LaTeX string
CRuntimeError: LaTeX was not found on the system
DNo error, plot displays correctly
Attempts:
2 left
💡 Hint
Check if LaTeX is installed and accessible by matplotlib.
🚀 Application
expert
3:00remaining
Creating a publication-quality plot with LaTeX labels and custom preamble
Which code snippet correctly sets a custom LaTeX preamble to use the 'amsmath' package and renders the plot with LaTeX labels?
A
import matplotlib.pyplot as plt
plt.rcParams['text.usetex'] = True
plt.rcParams['text.latex.preamble'] = r'\usepackage{amsmath}'
plt.plot([0, 1], [0, 1])
plt.xlabel(r'\alpha + \beta')
plt.show()
B
import matplotlib.pyplot as plt
plt.rcParams.update({
    'text.usetex': True,
    'text.latex.preamble': '\usepackage{amsmath}',
})
plt.plot([0, 1], [0, 1])
plt.xlabel('$\alpha + \beta$')
plt.show()
C
import matplotlib.pyplot as plt
plt.rcParams['text.usetex'] = True
plt.rcParams['text.latex.preamble'] = '\usepackage{amsmath}'
plt.plot([0, 1], [0, 1])
plt.xlabel('$\alpha + \beta$')
plt.show()
D
import matplotlib.pyplot as plt
plt.rcParams.update({
    'text.usetex': True,
    'text.latex.preamble': r'\usepackage{amsmath}',
})
plt.plot([0, 1], [0, 1])
plt.xlabel(r'$\alpha + \beta$')
plt.show()
Attempts:
2 left
💡 Hint
Use raw strings for LaTeX preamble and labels to avoid escape issues.