How to specify x and y axis for plotting dataframe

Question:

I would like to spcify x and y axis to draw data in dataframe in Python.

For example, I have four columns in dataframe. And here’s my code.

df.plot(x=df['a'], y=df['b'], label=df['c']) 

It throws an error saying: These values come from ‘b’ column.

“KeyError: ‘[ 500.8 567.2 487.2 444.4 1371.6 714.4 1157.4
476.8 345.4n 1076.4 881.8 813. 452.6 663.6 606.8 469.2 805.2 487.4n 497.8 440. 127. 68.6 1494.2 716.4 1447. 97.8 110.n 1126.4 1422.8 92.4 1000.8] not in index'”

Thank you for the help in advance.

Asked By: ejshin1

||

Answers:

The correct use is to pass column names:

df.plot(x='a', y='b', label='c')

The error is pandas trying to use the entire column df['b'] as a column name.

Answered By: IanS

Its a simple mistake. Instead of df['a'] … just past the column name i.e

df.plot(x='a', y='b', label='c') 
Answered By: Bharath

df.plot takes the column labels as x and y, not the data itself

df.plot(x='a', y='b')
might work

Use https://pandas.pydata.org/pandas-docs/stable/generated/pandas.DataFrame.plot.html as reference to the arguments needed in pandas

https://pandas.pydata.org/pandas-docs/stable/visualization.html as examples

Answered By: Trion
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.