What is legend function in Matplotlib ?
In Matplotlib, the `legend` function is used to add a legend to a plot. A legend provides information about the elements in the plot, associating labels with specific plot elements such as lines or markers. Here's a simple example: ```python import matplotlib.pyplot as plt # Create some data for plotting x = [1, 2, 3, 4] y1 = [2, 4, 6, 8] y2 = [1, 2, 1, 2] # Plot the data plt.plot(x, y1, label='Line 1') plt.plot(x, y2, label='Line 2') # Add a legend plt.legend() # Show the plot plt.show() ``` In this example, the `label` parameter is used in the `plot` function to assign labels to each line. The `legend` function then adds a legend to the plot based on these labels. The legend helps identify which line corresponds to which dataset.