[英]How to plot line plot with 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=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 45 45 45
我正在嘗試使用下面的命令進行繪制,但我正在獲取錯誤消息ValueError:數組的長度必須相同
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
調用,方法是將寬數據融合為長數據並將索引呈現為列。 然后調用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')
數據
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.