arrow
arrow
arrow
What is the purpose of the legend() function in Matplotlib?
Question

What is the purpose of the legend() function in Matplotlib?

A.

To add a title to a plot

B.

To add annotations to a plot

C.

To label different lines or markers on a plot

D.

To label the x and y axes of a plot

Correct option is C


The purpose of the legend() function in Matplotlib is to label different lines or markers on a plot. It is used to create a legend that helps identify what each line, marker, or color represents in a plot. This is particularly useful when multiple datasets or lines are plotted on the same graph, allowing users to distinguish between them.
Important Key Points:
1. Legend for Labels: The legend() function is used to display a legend, which provides labels for different elements (lines, markers, etc.) in the plot.
2. Automatic Labels: When plotting, you can assign labels to elements (e.g., lines or markers) using the label parameter in the plotting function, and then call legend() to display the legend.
3. Customizing Legend: You can customize the legend's position, font size, and other properties to improve readability.
4. Enhances Plot Readability: Adding a legend is essential when there are multiple datasets or categories, as it helps viewers understand the plot better.
Knowledge Booster:
· Option (a): The title() function is used to add a title to a plot, not the legend() function.
· Option (b): While annotations are added using the annotate() function in Matplotlib, legend() is specifically for labeling elements in the plot.
· Option (d): The xlabel() and ylabel() functions are used to label the x and y axes of a plot, respectively. The legend() function does not serve this purpose.

Free Tests

Free
Must Attempt

UPTET Paper 1: PYP Held on 23rd Jan 2022 (Shift 1)

languageIcon English
  • pdpQsnIcon150 Questions
  • pdpsheetsIcon150 Marks
  • timerIcon150 Mins
languageIcon English
Free
Must Attempt

UPTET Paper 2 Social Science : PYP Held on 23rd Jan 2022 (Shift 2)

languageIcon English
  • pdpQsnIcon150 Questions
  • pdpsheetsIcon150 Marks
  • timerIcon150 Mins
languageIcon English
Free
Must Attempt

UPTET Paper 2 Maths & Science : PYP Held on 23rd Jan 2022 (Shift 2)

languageIcon English
  • pdpQsnIcon150 Questions
  • pdpsheetsIcon150 Marks
  • timerIcon150 Mins
languageIcon English
test-prime-package

Access ‘EMRS PGT’ Mock Tests with

  • 60000+ Mocks and Previous Year Papers
  • Unlimited Re-Attempts
  • Personalised Report Card
  • 500% Refund on Final Selection
  • Largest Community
students-icon
353k+ students have already unlocked exclusive benefits with Test Prime!
Our Plans
Monthsup-arrow