how to add legend to python plot

import numpy as np
import matplotlib.pyplot as plt

x = np.linspace(0, 20, 1000)
y1 = np.sin(x)
y2 = np.cos(x)

plt.plot(x, y1, "-b", label="sine")
plt.plot(x, y2, "-r", label="cosine")
plt.legend(loc="upper left")
plt.ylim(-1.5, 2.0)
plt.show()

Here is what the above code is Doing:
1. We create a list of numbers from 0 to 20, with 1000 elements (x).
2. We create two lists of numbers, y1 and y2, using the sine and cosine functions.
3. We plot the sine and cosine functions.
4. We add a legend to the plot.
5. We set the range for the y-axis.
6. We display the plot.