所以我想绘制正态分布图,我看到一种方法是使用以下代码:
import numpy as np
import matplotlib.pyplot as plt
mu = 5
sigma = 1
s = np.random.normal(mu, sigma, 1000)
count, bins, ignored = plt.hist(s, 100, normed=True);
pdf = 1/(sigma * np.sqrt(2 * np.pi)) * np.exp(- (bins - mu)**2 / (2 * sigma**2))
mu_ = 10
sigma_ = 1
s = np.random.normal(mu_, sigma_, 1000)
count_, bins_, ignored_ = plt.hist(s, 100, normed=True);
pdf_ = 1/(sigma_ * np.sqrt(2 * np.pi)) * np.exp(- (bins_ - mu_)**2 / (2 * sigma_**2))
plt.plot(bins, pdf, linewidth=2, color='g')
plt.plot(bins_, pdf_, linewidth=2, color='r')
plt.show()
结果是:
我的问题是,我能否以某种方式隐藏直方图以便仅显示正态分布线??我知道还有另一种绘制正态分布的方法,但我更喜欢这种方法
谢谢你的帮助!!!
最佳答案
获得苹果片的一种可能方法当然是准备一个苹果派,然后从派上摘下所有的苹果。更简单的方法肯定是根本不做蛋糕。
因此,在图中不绘制直方图的明显方法是首先不绘制它。相反,使用 numpy.histogram
计算直方图(无论如何都是 function called by plt.hist
),并将其输出绘制到图中。
import numpy as np import matplotlib.pyplot as plt mu = 5 sigma = 1 s = np.random.normal(mu, sigma, 1000) count, bins = np.histogram(s, 100, normed=True) pdf = 1/(sigma * np.sqrt(2 * np.pi)) * np.exp(- (bins - mu)**2 / (2 * sigma**2)) mu_ = 10 sigma_ = 1 s = np.random.normal(mu_, sigma_, 1000) count_, bins_ = np.histogram(s, 100, normed=True) pdf_ = 1/(sigma_ * np.sqrt(2 * np.pi)) * np.exp(- (bins_ - mu_)**2 / (2 * sigma_**2)) plt.plot(bins, pdf, linewidth=2, color='g') plt.plot(bins_, pdf_, linewidth=2, color='r') plt.show()
关于python - 隐藏直方图,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40647396/