Nicholas
Nicholas

Reputation: 3737

How to add Legend to x.plot() - Python

I have a dataframe that I have turned into a line chart. I can do it with matplotlib, but I like the look of the simple 'x.plot()' functionality.

So my code looks like this:

x = lineFDF[threeYr]
y = lineFDF[twoYr]
a = lineFDF[oneYr]
b = lineFDF[Yr]
x.plot()
y.plot()
a.plot()
b.plot()

Which produces this:

enter image description here

How do I add a legend for it using the column headers?

Upvotes: 0

Views: 46

Answers (1)

sascha
sascha

Reputation: 33532

Something like:

x.plot(label='mylabel')
...
plt.legend()  # assuming you imported like: import matplotlib.pyplot as plt

But as your code is quite incomplete, there is nothing to test. (It also looks like you are using pandas, which wraps matplotlib; would have not hurt to mention stuff like that)

Upvotes: 2

Related Questions