Manually add legend Items Python Matplotlib

Using plt.legend() method, we can create a legend, and passing frameon would help to keep the border over there.


  • Set the X-axis label using plt.xlabel() method.

  • Set the Y-axis label using plt.ylabel() method.

  • Draw lines using plot() method.

  • Location and legend drawn flags can help to find a location and make the flag True for the border.

  • Set the legend with “blue” and “orange” elements.

  • To show the figure use method.


import matplotlib.pyplot as plt

plt.ylabel("Y-axis ")
plt.xlabel("X-axis ")

plt.plot([9, 5], [2, 5], [4, 7, 8])

location = 0 # For the best location
legend_drawn_flag = True
plt.legend(["blue", "orange"], loc=0, frameon=legend_drawn_flag)