What does plt.legend( ) function do?
What does plt.legend( ) function do?
Solution
✅ Correct Option: 2
In Matplotlib, plt.legend() displays a legend, a box that identifies each plotted line or marker using the label given in the plotting call, for example plt.plot(x, y, label="Sales").
Option 1: axis labels are added with plt.xlabel() and plt.ylabel().
Option 3: the title is added with plt.title().
plt.legend() shows only the lines that have a label, so without labels the legend is empty.
Related questions:
2025: 22 May Shift 1
2026: 26 May Shift 1