I wrote here how to superimpose the cumulative ratio line on pyplot.hist ()
. This time, I will overlay the cumulative ratio on multiple histograms drawn using seaborn.FacetGrid ()
. Below is the completed plot.
What you do is the same as here, but if you draw a histogram using seaborn.FacetGrid ()
or seaborn.distplot ()
, pyplot.hist ()
does not explicitly receive bin and frequency information as a return value. Therefore, the bin and frequency information is extracted from the ʻaxobject. Specifically, it retrieves the required information from the
patches object contained in ʻax
. The basic code is below
"""Overlay the cumulative ratio line on the FacetGrid histogram"""
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns
# % matplotlib inline
sns.set(style="darkgrid", palette="muted", color_codes=True)
#Toy data generation
np.random.seed(0)
size = 200
x1 = np.random.normal(size=size)
group = np.random.choice((0, 1), size=size)
df = pd.DataFrame({'x1': x1, 'group': group})
# seaborn.FacetGrid()Generate multiple histograms with
g = sns.FacetGrid(data=df, col='group', hue='group', size=5)
g = g.map(plt.hist, 'x1', alpha=0.7, label='Frequency')
#Draw a cumulative line for all ax contained in FacetGrid
for ax in g.axes.ravel():
#Extract patches
patches = ax.patches
#Calculate the value of the 2nd axis (x)
bins_part = [patch.get_xy()[0] + patch.get_width() for patch in patches]
bins = [patches[0].get_xy()[0]] + bins_part
x2 = np.convolve(bins, np.ones(2) / 2, mode="same")[1:]
#Calculate the value of the 2nd axis (y:Accumulation)
n = [patch.get_height() for patch in patches]
y2 = np.add.accumulate(n) / sum(n)
#2nd axis plot
ax2 = ax.twinx()
ax2.plot(x2, y2, ls='--', marker='o', color='r',
label='Cumulative ratio')
ax2.grid(visible=False)
plt.tight_layout()
plt.show()
The full code, including the legend, is in Gist. Please go to here.