Challenge - 5 Problems
LaTeX Plot Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2: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')
Attempts:
2 left
💡 Hint
Look at how raw strings and LaTeX math mode are used in matplotlib titles.
✗ Incorrect
Matplotlib supports LaTeX math formatting in titles when the string is prefixed with r and enclosed in dollar signs. This renders the title as a math expression.
❓ data_output
intermediate2: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'])
Attempts:
2 left
💡 Hint
Check the value of plt.rcParams['font.family'] after update.
✗ Incorrect
The 'font.family' key is set to 'serif', which is a generic family. The specific serif font 'Palatino' is set in 'font.serif' but plt.rcParams['font.family'] returns the generic family list.
❓ visualization
advanced3: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()
Attempts:
2 left
💡 Hint
Raw strings (r'') help avoid escape sequence errors in LaTeX strings.
✗ Incorrect
Using a raw string with r'' and enclosing the LaTeX math expression in dollar signs ensures correct rendering in matplotlib legends.
🔧 Debug
advanced2: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()
Attempts:
2 left
💡 Hint
Check if LaTeX is installed and accessible by matplotlib.
✗ Incorrect
If LaTeX is not installed or not in the system path, matplotlib raises a RuntimeError when usetex=True is set.
🚀 Application
expert3: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?
Attempts:
2 left
💡 Hint
Use raw strings for LaTeX preamble and labels to avoid escape issues.
✗ Incorrect
Option A correctly uses raw strings for the LaTeX preamble and label, ensuring proper escaping and rendering with usetex=True.