Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to specify x and y axis for plotting dataframe in Python

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.4\n 1076.4 881.8 813. 452.6 663.6 606.8 469.2 805.2 487.4\n 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.

like image 523
ejshin1 Avatar asked Aug 17 '17 15:08

ejshin1


People also ask

How do you label X and Y-axis in pandas?

You can set the labels on that object. Or, more succinctly: ax. set(xlabel="x label", ylabel="y label") . Alternatively, the index x-axis label is automatically set to the Index name, if it has one.

How do you change the axis of a DataFrame in Python?

DataFrame - set_axis() function The set_axis() function is used to assign desired index to given axis. Indexes for column or row labels can be changed by assigning a list-like or Index. The values for the new index. The axis to update.


1 Answers

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

like image 58
Trion Avatar answered Oct 11 '22 23:10

Trion