Bird
0
0

Which of the following is the correct syntax to add a legend with labels in matplotlib?

easy📝 Syntax Q3 of 15
Matplotlib - Seaborn Integration
Which of the following is the correct syntax to add a legend with labels in matplotlib?
Aplt.legend(labels='Data1, Data2')
Bplt.legend(label=['Data1', 'Data2'])
Cplt.legend(['Data1', 'Data2'])
Dplt.legend('Data1', 'Data2')
Step-by-Step Solution
Solution:
  1. Step 1: Recall correct legend syntax

    The plt.legend() function accepts a list of labels as its first argument to display in the legend.
  2. Step 2: Check each option

    Only plt.legend(['Data1', 'Data2']) correctly passes a list of labels. The other options use incorrect parameter names or argument formats.
  3. Final Answer:

    plt.legend(['Data1', 'Data2']) -> Option C
  4. Quick Check:

    Legend labels = list in plt.legend() [OK]
Quick Trick: Pass a list of labels to plt.legend() [OK]
Common Mistakes:
  • Using wrong parameter name 'label'
  • Passing string instead of list
  • Passing multiple string arguments

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Matplotlib Quizzes