Bird
0
0

You want to create a plot with the title showing the equation E = mc^2 using LaTeX in matplotlib. Which code snippet correctly achieves this and saves the plot as a PDF with LaTeX-rendered text?

hard📝 Application Q15 of 15
Matplotlib - Export and Publication Quality
You want to create a plot with the title showing the equation E = mc^2 using LaTeX in matplotlib. Which code snippet correctly achieves this and saves the plot as a PDF with LaTeX-rendered text?
Aplt.rcParams['text.usetex'] = True plt.title(r'$E = mc^2$') plt.savefig('energy.pdf')
Bplt.rcParams['text.usetex'] = True plt.title('E = mc^2') plt.savefig('energy.pdf')
Cplt.title(r'$E = mc^2$') plt.savefig('energy.pdf')
Dplt.rcParams['text.usetex'] = False plt.title(r'$E = mc^2$') plt.savefig('energy.pdf')
Step-by-Step Solution
Solution:
  1. Step 1: Enable LaTeX rendering

    Set plt.rcParams['text.usetex'] = True to use LaTeX for all text rendering.
  2. Step 2: Use raw string with math delimiters for title

    Title must be a raw string with LaTeX math mode: r'$E = mc^2$'.
  3. Step 3: Save plot as PDF

    Use plt.savefig('energy.pdf') to save the plot with LaTeX-rendered title.
  4. Final Answer:

    plt.rcParams['text.usetex'] = True plt.title(r'$E = mc^2$') plt.savefig('energy.pdf') -> Option A
  5. Quick Check:

    usetex=True + raw string + $...$ + save = correct [OK]
Quick Trick: Enable usetex and use raw string with $...$ for LaTeX titles [OK]
Common Mistakes:
  • Not enabling usetex before plotting
  • Missing raw string prefix r
  • Not using $ to mark LaTeX math mode

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Matplotlib Quizzes