简体   繁体   English

如何用seaborn绘制线图?

[英]How to plot line plot with seaborn?

I have a sample data frame shown below. 我有一个如下所示的示例数据框。 I want to plot each column against the data frame index. 我想针对数据帧索引绘制每一列。

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)  

using x1.head() the output is shown below. 使用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

I checked the output of each column, they are all equal. 我检查了每一列的输出,它们都是相等的。

print(len(x1.index), len(x1['x25']), len(x1['x50']), len(x1['x75']), len(x1['x100']), len(x1['x125']))

45 45 45 45 45 45 45 45 45 45 45 45

I am trying to plot with the command below, but i am getting The error message ValueError: arrays must all be same length 我正在尝试使用下面的命令进行绘制,但我正在获取错误消息ValueError:数组的长度必须相同

sns.lineplot( x1, x1.index, ['x25','x50','x75','x100','x125'])

could somebody please let me know, what i am doing wrong. 有人可以让我知道,我做错了什么。

Consider calling lineplot multiple times, passing in object such as pandas series to named arguments: 考虑多次调用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'])

Or in loop: 或循环:

for i in  ['x25','x50','x75','x100','x125']:
   sns.lineplot(x=x1.index, y=x1[i])

多线图输出


However, consider using a single data frame and hence single lineplot call by melting your wide data to long and rendering your index as a column. 但是,请考虑使用单个数据帧,因此考虑使用单个lineplot调用,方法是将宽数据融合为长数据并将索引呈现为列。 Then call lineplot with hue for automatic legend: 然后调用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')

单数据帧图


Data 数据

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM