Rob Edris
Rob Edris

Reputation: 1

How to plot many columns against each other in the same line-plot using pandas?

Dataset:

Dataset

I want to use 'Filename' as the x axis, and every column ending with .csv as the y data on top of each other.

Hopeful end result:

Hopeful end result

I am trying to achieve a result like this but so far I have no luck.

## packages used
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import seaborn as sns
import re
QtzLOD1 = pd.read_csv (r"doc.csv")

x = QtzLOD1["filename"]
y = QtzLOD1.iloc[0:,1:14]
for i in range(len(x)):
    plt.plot(x[i], y[i], marker=".")

fig=plt.plot()
fig.savefig(p)

I am getting keyerror 0 any help is much appreciated thanks!

Upvotes: 0

Views: 49

Answers (1)

Hosein Asilian
Hosein Asilian

Reputation: 41

It is hard to say without know how your data is structured in the *.csv file. But From my understanding the problem is that x[i] would be a Series, but y[i] can be an slice of the data frame (basically a DataFrame). You may need to use .to_numpy() method to convert it to an array and then working from there. But still you need to pay attention that x and y in plot should be the same size and dimension. I'm giving you an example that may help, similar to your data.

import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
element=["Be" ,"Na" ,"Ti", "Rb"]
test1=list(np.random.randint(1,1000,4))
test2=list(np.random.randint(1,1000,4))
test3=list(np.random.randint(1,1000,4))
test4=list(np.random.randint(1,1000,4))
test5=list(np.random.randint(1,1000,4))
data=  {"element":element,"test1":test1,"test2":test2,\
"test3":test3,"test4":test4,"test5":test5}
data_frame1=pd.DataFrame(data)
x=data_frame1["element"].to_numpy()
y=data_frame1.iloc[1:,0:]
num_test=len(data_frame1.iloc[0,1:])
for i in range(5):
   y=data_frame1.iloc[::,i+1].to_numpy()
   plt.plot(x,y)
plt.show()

Upvotes: 0

Related Questions