Skip to content

Instantly share code, notes, and snippets.

@LimHyungTae
Created February 4, 2020 12:57
Show Gist options
  • Save LimHyungTae/29478546c78fc7ef94c431d609b1b0db to your computer and use it in GitHub Desktop.
Save LimHyungTae/29478546c78fc7ef94c431d609b1b0db to your computer and use it in GitHub Desktop.
Draw histogram on Python
import h5py
import os
import numpy as np
import cv2
import matplotlib.pyplot as plt
from scipy import ndimage, misc
from scipy.stats import norm
def draw_histogram(x, n_bins, name):
n, bins, patches = plt.hist(x, bins=n_bins, color=(0.0, 0.0, 0.5), density=True)
plt.cla()
fig = plt.figure()
weights = np.ones_like(x)/float(len(x))
plt.hist(x, bins=n_bins, color=(0.0, 0.0, 0.5), weights=weights)
''' For gaussian fitting'''
xt = plt.xticks()[0]
xmin, xmax = min(xt), max(xt)
lnspc = np.linspace(xmin, xmax, len(x))
# lets try the normal distribution first
m, s = norm.fit(x) # get mean and standard deviation
pdf_g = norm.pdf(lnspc, m, s) # now get theoretical values in our interval
pdf_g = pdf_g/sum(n)
plt.plot(lnspc, pdf_g, label="Norm", color='r', linestyle='--') # plot it
print(m, s)
# plt.title(r'$ \mu=100,\ \sigma=15$')
plt.title(r'$ \mu={},\ \sigma={}$'.format(round(m, 3), round(s, 3)))
# plt.title("Mean: " + str(round(m, 2)) + "Std: " + str(round(s,2)))
plt.xlabel("Measured Y [m]")
plt.ylabel("# of points")
# (mu, sigma) = norm.fit(x)
# gaussian_line = mlab.normpdf(n_bins, mu, sigma)
# plt.plot(n_bins, gaussian_line, 'r', linewidth=2)
fig = plt.gcf()
fig.savefig("histo_{}.png".format(name))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment