Python 如何用seaborn绘制线条图?
我有一个示例数据框,如下所示。我想根据数据帧索引绘制每一列Python 如何用seaborn绘制线条图?,python,seaborn,Python,Seaborn,我有一个示例数据框,如下所示。我想根据数据帧索引绘制每一列 x1 = pd.DataFrame() x1['x25'] = df['X'][0:45].reset_index(drop=True) x1['x50'] = df['X'][90:135].reset_index(drop=True) x1['x75'] = df['X'][180:225].reset_index(drop=True) x1['x100'] = df['X'][270:315].reset_index(drop=T
x1 = pd.DataFrame()
x1['x25'] = df['X'][0:45].reset_index(drop=True)
x1['x50'] = df['X'][90:135].reset_index(drop=True)
x1['x75'] = df['X'][180:225].reset_index(drop=True)
x1['x100'] = df['X'][270:315].reset_index(drop=True)
x1['x125'] = df['X'][360:405].reset_index(drop=True)
使用x1.head,输出如下所示
x25 x50 x75 x100 x125
0 22732 22852 22997 23151 23253
1 22732 22853 22995 23153 23254
2 22733 22851 22997 23153 23254
3 22731 22851 22995 23150 23255
4 22730 22851 22997 23152 23254
我检查了每列的输出,它们都是相等的
print(len(x1.index), len(x1['x25']), len(x1['x50']), len(x1['x75']), len(x1['x100']), len(x1['x125']))
45 45 45
我试图使用下面的命令进行绘图,但得到的错误消息是ValueError:arrays的长度必须相同
sns.lineplot( x1, x1.index, ['x25','x50','x75','x100','x125'])
谁能告诉我我做错了什么。考虑多次调用lineplot,将pandas系列之类的对象传递给命名参数:
sns.lineplot(x=x1.index, y=x1['x25'])
sns.lineplot(x=x1.index, y=x1['x50'])
sns.lineplot(x=x1.index, y=x1['x75'])
sns.lineplot(x=x1.index, y=x1['x100'])
sns.lineplot(x=x1.index, y=x1['x125'])
或在循环中:
for i in ['x25','x50','x75','x100','x125']:
sns.lineplot(x=x1.index, y=x1[i])
但是,考虑使用单个数据帧,因此,通过将宽数据熔化为长列并将索引呈现为列,使用单行图调用。然后调用带有色调的lineplot以显示自动图例:
# CREATE NEW COLUMN NAMED index
x1 = x1.reset_index()
# MELT DATA
mdf = x1.melt(id_vars='index')
# PLOT LINE WITH data AND hue ARGUMENTS
sns.lineplot(x='index', y='value', data=mdf, hue='variable')
资料
检查如何使用lineplot。
df = pd.DataFrame({'X': np.random.uniform(2000, 5000, 500)})
x1 = pd.DataFrame({'x25': df['X'][0:45].reset_index(drop=True),
'x50': df['X'][90:135].reset_index(drop=True),
'x75': df['X'][180:225].reset_index(drop=True),
'x100': df['X'][270:315].reset_index(drop=True),
'x125': df['X'][360:405].reset_index(drop=True)})