alessandro
alessandro

Reputation: 3984

add legend to pyplot, when using single plot call

I'm used to this syntax when plotting multiple lines:

plt.plot(freqs,ps,freqs,psf)

I tried different variations of label=["ps","psf"] in it, using different kind of parentheses, but I was always unable to obtain a correct legend

Upvotes: 1

Views: 141

Answers (1)

Mel
Mel

Reputation: 6065

plt.plot will return a list of handles for the lines. You can pass those to legend, along with a list of labels:

handles=plt.plot([0,1],[5,6],[0,1],[8,7])
plt.legend(handles,["label a","label b"])

Upvotes: 2

Related Questions