Search code examples
pythonplotmatplotlibdiagram

matplotlib.pyplot How to name different lines in the same plot?


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?


Solution

  • 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()