Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 952
erikbstack Avatar asked Jun 18 '12 12:06

erikbstack


People also ask

How do you plot multiple lines on one plot in Python?

You can plot multiple lines from the data provided by an array in python using matplotlib. You can do it by specifying different columns of the array as the x and y-axis parameters in the matplotlib. pyplot. plot() function.

Can I have two legends in matplotlib?

Multiple Legends. Sometimes when designing a plot you'd like to add multiple legends to the same axes. Unfortunately, Matplotlib does not make this easy: via the standard legend interface, it is only possible to create a single legend for the entire plot. If you try to create a second legend using plt.


2 Answers

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()
like image 144
Simon Bergot Avatar answered Oct 01 '22 17:10

Simon Bergot


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.

like image 39
Benjamin Bannier Avatar answered Oct 01 '22 17:10

Benjamin Bannier