Reputation: 5188
I have a Pandas DataFrame which resembles:
pd.DataFrame({
'el1': {
'steps': [0,1,2],
'values': [10, 9, 8]
},
'el2': {
'steps': [0,1,2],
'values': [1, 2, 8]
},
'el3': {
'steps': [0,1,2],
'values': [5, 9, 4]
}
})
el1 el2 el3
steps [0, 1, 2] [0, 1, 2] [0, 1, 2]
values [10, 9, 8] [1, 2, 8] [5, 9, 4]
what would be the best way to try and use Panda's DataFrame plot to get some simple line plots with values
on the y
axis and steps
on the x
axis? (e.g. there should be three lines)
Upvotes: 1
Views: 2514
Reputation: 3967
Use matplotlib
c = ['r', 'g', 'b']
for i in range(df.shape[1]):
plt.plot(df.iloc[0, i], df.iloc[1, i], c=c[i], label=df.columns[i])
plt.legend(df.columns)
plt.xlabel('Steps')
plt.ylabel('Values')
plt.show()
Upvotes: 1
Reputation: 1842
Here's a different construction. Largely because I'm more comfortable with transforming the underlying data frame first before plotting. The plotting of the graph is pretty much the same so, credits go to @meW for the lines of code.
import pandas as pd
import matplotlib.pyplot as plt
df = pd.DataFrame({
'el1': {
'steps': [0,1,2],
'values': [10, 9, 8]
},
'el2': {
'steps': [0,1,2],
'values': [1, 2, 8]
},
'el3': {
'steps': [0,1,2],
'values': [5, 9, 4]
}
})
ndf = pd.DataFrame({v:df[v].values[1] for v in df.columns})
ndf.index = df.el1.steps
ndf.columns = df.columns
>>>ndf
el1 el2 el3
0 10 1 5
1 9 2 9
2 8 8 4
plt.plot(ndf)
plt.legend(ndf.columns)
plt.xlabel('Steps')
plt.ylabel('Values')
plt.show()
Upvotes: 1