Skip to content

Instantly share code, notes, and snippets.

@gpratt
Created October 29, 2015 16:45
Show Gist options
  • Save gpratt/07990a1ce76bcf483595 to your computer and use it in GitHub Desktop.
Save gpratt/07990a1ce76bcf483595 to your computer and use it in GitHub Desktop.
Plots a PDF
def pdf(data, bins=50):
data = np.array(data, dtype=float)
minimum = np.min(data) - .000001
maximum = np.max(data) + .000001
pos = np.linspace(minimum, maximum, bins + 1)
xs = np.linspace(minimum, maximum, bins + 1)[:-1]
ys = np.linspace(minimum, maximum, bins + 1)[1:]
pdf = np.ndarray(shape=(bins + 1, 1))
pdf[0] = 0
for i, (x, y) in enumerate(zip(xs, ys)):
region = len(data[np.where((data >= x) & (data < y))])
prob = region / float(len(data))
pdf[i + 1] = prob
return pos, pdf
def plot_pdf(data, bins=50, ax=None, **kwargs):
if ax is None:
ax = plt.gca()
x, y = pdf(data, bins=bins)
ax.plot(x, y, **kwargs)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment