How to draw probability distribution histogram by Python
Xiaobian to share with you how Python draws probability distribution histogram, I believe most people still do not know how, so share this article for your reference, I hope you have a lot of gains after reading this article, let's go to understand it together!
preface
Histogram is a tool that can quickly show the probability distribution of data, intuitive and easy to understand, and loved by data enthusiasts. You may usually see at most matplotlib, seaborn and other high-level package libraries, similar to the following drawings.
I. Code
1. Case 1
import randomimport numpy as npimport matplotlib.pyplot as pltX = np.random.normal(loc=10, scale=2, size=(1, 1000))[0]Y = np.random.normal(loc=10, scale=1.0, size=(1, 1000))[0]P = np.ones(1000)P[:600] = 0np.random.shuffle(P)# print(P)Z = X + Y * Pplt.hist(Z, density=True, bins=150)plt.show()
2. Case 2
import randomimport numpy as npimport matplotlib.pyplot as pltn = 1000# p = 0.4U = []for i in range(1000): X = np.random.normal(loc=10, scale=2, size=(1, n))[0] Y = np.random.normal(loc=10, scale=1.0, size=(1, n))[0] P = np.ones(n) P[:600] = 0 np.random.shuffle(P) Z = X + Y * P a = 1 / pow(n * np.var(Z), 1/2) b = sum(Z) - n * np.mean(Z) u = a * b U.append(u)plt.hist(U, density=True, bins=60)plt.show()
1. Case 1
2. Case 2
That's all for "How Python draws probability distribution histograms." Thank you for reading! I believe that everyone has a certain understanding, hope to share the content to help everyone, if you still want to learn more knowledge, welcome to pay attention to the industry information channel!