A. Missing raw string prefix causing backslash escape error
B. Legend function is missing parentheses
C. Label string should not use dollar signs
D. Plot data is invalid
Solution
Step 1: Check string for LaTeX math in label
Using "$\alpha$" without raw string causes Python to treat \a as escape sequence.
Step 2: Add raw string prefix to fix backslash escape
Change label to r"$\alpha$" to correctly render Greek letter alpha.
Final Answer:
Missing raw string prefix causing backslash escape error -> Option A
Quick Check:
Use raw string for LaTeX labels [OK]
Hint: Always use raw strings for LaTeX in labels [OK]
Common Mistakes:
Forgetting raw string prefix
Removing dollar signs disables math mode
Misunderstanding legend syntax
5. You want to label the y-axis with the formula for the quadratic roots: \(x = \frac{-b \pm \sqrt{b^2 - 4ac}}{2a}\) Which matplotlib code correctly displays this formula using LaTeX?
hard
A. plt.ylabel(r"x = \frac{-b \pm \sqrt{b^2 - 4ac}}{2a}")
B. plt.ylabel("$x = \frac{-b \pm \sqrt{b^2 - 4ac}}{2a}$")
C. plt.ylabel(r"$x = \frac{-b \pm \sqrt{b^2 - 4ac}}{2a}$")
D. plt.ylabel("x = \frac{-b \pm \sqrt{b^2 - 4ac}}{2a}")
Solution
Step 1: Use raw string to avoid backslash escape errors
The formula contains many backslashes, so raw string (r"...") is needed.
Step 2: Wrap the entire formula in dollar signs for math mode
Dollar signs enable LaTeX math rendering in matplotlib labels.
Final Answer:
plt.ylabel(r"$x = \frac{-b \pm \sqrt{b^2 - 4ac}}{2a}$") -> Option C
Quick Check:
Raw string + $...$ = Correct LaTeX formula [OK]
Hint: Use raw string and $...$ to display complex formulas [OK]
Common Mistakes:
Not using raw string causes syntax errors
Missing dollar signs disables math rendering
Using plain strings with backslashes
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.