Python 数据帧图:值列表

Python 数据帧图:值列表,python,pandas,seaborn,Python,Pandas,Seaborn,我有一个熊猫数据框,类似于: pd.DataFrame({ “el1”:{ “步骤”:[0,1,2], “值”:[10,9,8] }, “el2”:{ “步骤”:[0,1,2], “值”:[1,2,8] }, “el3”:{ “步骤”:[0,1,2], “值”:[5,9,4] } }) 使用Panda的DataFrame plot,在y轴和x轴上分别绘制值和步数的简单线条图,最好的方法是什么?(例如,应该有三行)使用matplotlib c = ['r', 'g', 'b'] for i in

我有一个熊猫数据框,类似于:

pd.DataFrame({
“el1”:{
“步骤”:[0,1,2],
“值”:[10,9,8]
},
“el2”:{
“步骤”:[0,1,2],
“值”:[1,2,8]
},
“el3”:{
“步骤”:[0,1,2],
“值”:[5,9,4]
}
})

使用Panda的DataFrame plot,在
y
轴和
x
轴上分别绘制
值和
步数的简单线条图,最好的方法是什么?(例如,应该有三行)

使用
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()

这里有一个不同的结构。这主要是因为我更习惯于在绘图之前先转换底层数据帧。图形的绘制几乎是一样的,因此,代码行的积分归@meW

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()

对不起,我本想编辑我自己的答案,但却编辑了你的答案。不知道如何拒绝它xD@kerwei我已经处理好了。
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()