24

Think about doing this:

import matplotlib.pyplot as plt

plt.plot(x_A,y_A,'g--')
plt.plot(x_B,y_B,'r-o')
plt.show()

How would you go about giving both lines different names, i.e. like Microsoft Excel would do it?

erikbstack
  • 12,878
  • 21
  • 81
  • 115
  • 1
    Another approach is to place the labels near the lines that they correspond to: https://stackoverflow.com/a/29724863/1959808 – 0 _ Sep 14 '17 at 11:09

2 Answers2

41
import matplotlib.pyplot as plt

plt.plot(x_A,y_A,'g--', label="plot A")
plt.plot(x_B,y_B,'r-o', label="plot A")
plt.legend()
plt.show()
Simon Bergot
  • 10,378
  • 7
  • 39
  • 55
6

You can give each line a label.

plt.plot(x_A,y_A,'g--', label='x_A')

These labels can then be displayed in a legend with

legend()

legend takes some arguments, see the documentation to see what it can do.

Benjamin Bannier
  • 55,163
  • 11
  • 60
  • 80