Search code examples
pythonpandashistogramkernel-density

Pandas histogram plot with kde?


I have a Pandas dataframe (Dt) like this:

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

From this i want to plot an histogram with kde for each column from C1 to C10 in an arrange just like the one that i obtain if i plot it with pandas,

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

enter image description here

But so far i've been not able to add the kde in each histogram; i want something like this:

enter image description here

Any ideas on how to accomplish this?


Solution

  • You want to first plot your histogram then plot the kde on a secondary axis.

    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


    How It Works

    • Keep track of total number of subplots

      k = len(df.columns)
      
    • n will be the number of chart columns. Change this to suit individual needs. m will be the calculated number of required rows based on k and n

      n = 2
      m = (k - 1) // n + 1
      
    • Create a figure and array of axes with required number of rows and columns.

      fig, axes = plt.subplots(m, n, figsize=(n * 5, m * 3))
      
    • Iterate through columns, tracking the column name and which number we are at i. Within each iteration, plot.

      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)
      
    • Use tight_layout() as an easy way to sharpen up the layout spacing

      fig.tight_layout()