Skip to content

Instantly share code, notes, and snippets.

@messefor
Created September 5, 2017 12:16
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save messefor/2f75e5d761da1dc7411bb24daf4afe76 to your computer and use it in GitHub Desktop.
Save messefor/2f75e5d761da1dc7411bb24daf4afe76 to your computer and use it in GitHub Desktop.
Histogram with cumulative ratio
"""Example to show how to plot histogram with accumulate ratio."""
import numpy as np
import matplotlib.pyplot as plt
import seaborn as sns
# % matplotlib inline
sns.set(style="darkgrid", palette="muted", color_codes=True)
# Toy data
np.random.seed(0)
dt = np.random.normal(size=100)
fig, ax1 = plt.subplots()
# Plot histogram and get bin info
n, bins, patches = ax1.hist(dt, alpha=0.7, label='Frequency')
# Add 1st y label
ax1.set_ylabel('Frequency')
# Get values plot with secondary axis
# Calc cumulative ratio
y2 = np.add.accumulate(n) / n.sum()
# Calc moving average
x2 = np.convolve(bins, np.ones(2) / 2, mode="same")[1:]
# Plot with secondary axes
ax2 = ax1.twinx()
lines = ax2.plot(x2, y2, ls='--', color='r', marker='o',
label='cumulative ratio')
ax2.grid(visible=False)
# Add 2nd y label
ax2.set_ylabel('cumulative ratio')
# Add Legend
plt.legend(handles=[patches[0], lines[0]])
plt.show()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment