2017-04-26 81 views
1

我有一個熊貓數據框(Dt)是這樣的:熊貓直方圖與kde的情節?

Pc  Cvt  C1 C2 C3 C4 C5 C6 C7 C8 C9 C10 
    0  1  2 0.08 0.17 0.16 0.31 0.62 0.66 0.63 0.52 0.38 
    1  2  2 0.09 0.15 0.13 0.49 0.71 1.28 0.42 1.04 0.43 
    2  3  2 0.13 0.24 0.22 0.17 0.66 0.17 0.28 0.11 0.30 
    3  4  1 0.21 0.10 0.23 0.08 0.53 0.14 0.59 0.06 0.53 
    4  5  1 0.16 0.21 0.18 0.13 0.44 0.08 0.29 0.12 0.52 
    5  6  1 0.14 0.14 0.13 0.20 0.29 0.35 0.40 0.29 0.53 
    6  7  1 0.21 0.16 0.19 0.21 0.28 0.23 0.40 0.19 0.52 
    7  8  1 0.31 0.16 0.34 0.19 0.60 0.32 0.56 0.30 0.55 
    8  9  1 0.20 0.19 0.26 0.19 0.63 0.30 0.68 0.22 0.58 
    9  10  2 0.12 0.18 0.13 0.22 0.59 0.40 0.50 0.24 0.36 
    10  11  2 0.10 0.10 0.19 0.17 0.89 0.36 0.65 0.23 0.37 
    11  12  2 0.19 0.20 0.17 0.17 0.38 0.14 0.48 0.08 0.36 
    12  13  1 0.16 0.17 0.15 0.13 0.35 0.12 0.50 0.09 0.52 
    13  14  2 0.19 0.19 0.29 0.16 0.62 0.19 0.43 0.14 0.35 
    14  15  2 0.01 0.16 0.17 0.20 0.89 0.38 0.63 0.27 0.46 
    15  16  2 0.09 0.19 0.33 0.15 1.11 0.16 0.87 0.16 0.29 
    16  17  2 0.07 0.18 0.19 0.15 0.61 0.19 0.37 0.15 0.36 
    17  18  2 0.14 0.23 0.23 0.20 0.67 0.38 0.45 0.27 0.33 
    18  19  1 0.27 0.15 0.20 0.10 0.40 0.05 0.53 0.02 0.52 
    19  20  1 0.12 0.13 0.18 0.22 0.60 0.49 0.66 0.39 0.66 
    20  21  2 0.15 0.20 0.18 0.32 0.74 0.58 0.51 0.45 0.37 
    . 
    . 
    . 

由此我想繪製與kdehistogramC1每列C10在安排就像我獲得,如果我的情節之一它與大熊貓,

Dt.iloc[:,2:].hist() 

enter image description here

但到目前爲止,我已經無法添加0每個直方圖中的;我想是這樣的:

enter image description here

如何做到這一點任何想法?

+0

你可以通過結合'distplot'(很明顯)和'FacetGrid.map_dataframe'實現sekorn,如[這裏]所述(http://seaborn.pydata.org/generated/seaborn.FacetGrid.html?highlight = map_dataframe)。 – IanS

+0

不要忘記[接受答案](http://stackoverflow.com/help/accepted-answer)。 – IanS

回答

5

您想先繪製您的直方圖,然後在輔助軸上繪製kde。

Minimal and Complete Verifiable Example MCVE

import pandas as pd 
import numpy as np 
import matplotlib.pyplot as plt 

df = pd.DataFrame(np.random.randn(1000, 4)).add_prefix('C') 

k = len(df.columns) 
n = 2 
m = (k - 1) // n + 1 
fig, axes = plt.subplots(m, n, figsize=(n * 5, m * 3)) 
for i, (name, col) in enumerate(df.iteritems()): 
    r, c = i // n, i % n 
    ax = axes[r, c] 
    col.hist(ax=ax) 
    ax2 = col.plot.kde(ax=ax, secondary_y=True, title=name) 
    ax2.set_ylim(0) 

fig.tight_layout() 

enter image description here


如何使用

  • 跟蹤次要情節總數的

    k = len(df.columns) 
    
  • n將是圖表列的數量。改變這個以適應個人需求。 m將基於k所需的行數的計算和n

    n = 2 
    m = (k - 1) // n + 1 
    
  • 創建figure和陣列的axes與所需數量的行和列的。

    fig, axes = plt.subplots(m, n, figsize=(n * 5, m * 3)) 
    
  • 通過迭代列,跟蹤列name和數量我們在i。在每次迭代中,繪圖。

    for i, (name, col) in enumerate(df.iteritems()): 
        r, c = i // n, i % n 
        ax = axes[r, c] 
        col.hist(ax=ax) 
        ax2 = col.plot.kde(ax=ax, secondary_y=True, title=name) 
        ax2.set_ylim(0) 
    
  • 使用tight_layout()作爲一種簡單的方法削尖的佈局間距

    fig.tight_layout() 
    
0

這裏是一個純seaborn解決方案,使用FacetGrid.map_dataframe所解釋here

竊取從@piRSquared的例子:

import pandas as pd 
import numpy as np 

df = pd.DataFrame(np.random.randn(1000, 4)).add_prefix('C') 

獲取所需格式的數據:

df = df.stack().reset_index(level=1, name="val") 

結果:

level_1  val 
0  C0 0.879714 
0  C1 -0.927096 
0  C2 -0.929429 
0  C3 -0.571176 
1  C0 -1.127939 

然後:

import seaborn as sns 

def distplot(x, **kwargs): 
    ax = plt.gca() 
    data = kwargs.pop("data") 
    sns.distplot(data[x], ax=ax, **kwargs) 

g = sns.FacetGrid(df, col="level_1", col_wrap=2, size=3.5) 
g = g.map_dataframe(distplot, "val") 

您可以根據需要調整col_wrap