Pandas柱状图与kde?

2024-07-07 07:34:58 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一个Pandas数据帧(Dt)如下:

  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
   .
   .
   .

从这里,我想用kdeC1到{}的每一列绘制一个histogram和{},就像我用pandas绘制的那样

^{pr2}$

enter image description here

但到目前为止,我还不能在每个柱状图中添加kde;我想要这样的东西:

enter image description here

有什么办法吗?在


Tags: 数据pandasdt绘制c2pcc1kde
2条回答

首先要绘制直方图,然后在次轴上绘制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将是基于kn计算出的所需行数

    n = 2
    m = (k - 1) // n + 1
    
  • 使用所需的行数和列数创建figureaxes数组。在

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

这是一个纯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')

获取所需格式的数据:

^{pr2}$

结果:

  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。在

相关问题 更多 >