Bird
0
0

Identify the error in this code snippet that tries to add a legend:

medium📝 Debug Q14 of 15
Matplotlib - Seaborn Integration
Identify the error in this code snippet that tries to add a legend:
import matplotlib.pyplot as plt
plt.plot([1, 2, 3], label='Line 1')
plt.legend()
plt.show()
AThe plot function is missing y-values
BThe legend function is called before plot
CThe label parameter is invalid in plot
DThere is no error; the code runs correctly
Step-by-Step Solution
Solution:
  1. Step 1: Check plot function parameters

    The plot call has only one list, so it treats it as y-values with x as indices 0,1,2.
  2. Step 2: Understand matplotlib behavior

    This is valid syntax; it plots y-values against default x-values. So no error here.
  3. Step 3: Re-examine options carefully

    The plot function is missing y-values says missing y-values, but y-values are given. The legend function is called before plot is wrong order. The label parameter is invalid in plot label is valid. There is no error; the code runs correctly says no error.
  4. Final Answer:

    There is no error; the code runs correctly -> Option D
  5. Quick Check:

    Code runs fine with legend after plot [OK]
Quick Trick: Check if plot syntax matches matplotlib docs [OK]
Common Mistakes:
  • Assuming single list plot is invalid
  • Thinking legend must come before plot
  • Believing label is not accepted in plot

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Matplotlib Quizzes