2017-04-12 79 views
1

我從3個不同的數據框(全部使用相同的鍵)導入數據,並將它們放在一起以形成單個數據框。使用pd.DataFrame中的MultiIndex繪製數據

df1 = read_xlsx('Means_Cent') 
df2 = read_xlsx('Means_Rand') 
df3 = read_xlsx('Means_Const') 
df1['Key'] = 'Cent' 
df2['Key'] = 'Rand' 
df3['Key'] = 'Const' 

df_means = pd.concat([df1,df2,df3], keys = ['Cent', 'Rand', 'Const']) 

現在我想創建使用DataFrame.plot()其中,I 1個具有圖表爲每個鍵=曲線[「美分」,「蘭德」,「CONST」〕中相同的數字。我的數據框df_means的

部分看起來是這樣的:

  02_VOI 03_Solidity 04_Total_Cells 
Cent 0 1.430  19.470   132.0 
     1 1.415  18.880   131.0 
     2 1.460  19.695   135.0 
     3 1.520  19.695   141.0 
Rand 0 1.430  19.205   132.0 
     1 1.430  19.170   132.0 
     2 1.445  19.430   133.5 
     3 1.560  19.820   144.5 
Const 0 1.175  22.695   108.5 
     1 1.430  22.260   132.0 
     2 1.180  21.090   109.0 
     3 1.360  22.145   126.0 

現在我要繪製02_VOI VS 04_Total_Cells,它應該是1個圖中每個鍵(G1 = 02_VOI(分)VS 04_Total_Cells(分) ,G2 = 02_VOI(RAND)VS 04_Total_Cells(RAND)...)

我嘗試使用DataFrame.unstack():

df_means.unstack(level = 0).plot(x = '02_VOI', y = '04_Total_Cells') 

但這似乎陷入困境的鑰匙。它會返回9個圖表(每個VOI(Cent,Rand,Const)和Total_Cells(Cent,Rand,Const)組合的1個組合)。 3個初始dataframes

回答

2

我想我會用Seaborn地塊這要容易得多Seaborn喜歡"tidy"數據

import pandas as pd 
import seaborn as sns 
df_mean = pd.read_clipboard() 
df_mean 

輸出:。

  02_VOI 03_Solidity 04_Total_Cells 
Cent 0 1.430  19.470   132.0 
     1 1.415  18.880   131.0 
     2 1.460  19.695   135.0 
     3 1.520  19.695   141.0 
Rand 0 1.430  19.205   132.0 
     1 1.430  19.170   132.0 
     2 1.445  19.430   133.5 
     3 1.560  19.820   144.5 
Const 0 1.175  22.695   108.5 
     1 1.430  22.260   132.0 
     2 1.180  21.090   109.0 
     3 1.360  22.145   126.0 

復位指數和再按照你的意願命名。

df_mean = df_mean.reset_index() 
df_mean = df_mean.rename(columns={'level_0':'Groups','level_1':'Samples'}) 
_ = sns.lmplot(x='02_VOI',y='04_Total_Cells', data=df_mean, scatter=True, col='Groups',fit_reg=False) 

enter image description here