python - 隐藏直方图

标签 python matplotlib plot histogram

所以我想绘制正态分布图,我看到一种方法是使用以下代码:

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()

结果是:

enter image description here

我的问题是,我能否以某种方式隐藏直方图以便仅显示正态分布线??我知道还有另一种绘制正态分布的方法,但我更喜欢这种方法

谢谢你的帮助!!!

最佳答案

获得苹果片的一种可能方法当然是准备一个苹果派,然后从派上摘下所有的苹果。更简单的方法肯定是根本不做蛋糕。

因此,在图中不绘制直方图的明显方法是首先不绘制它。相反,使用 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()

enter image description here

关于python - 隐藏直方图,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40647396/

相关文章:

python - 如何使用 get_object_or_404 排除结果?

python - 根据查找 df 矩阵从一个 df 获取行

python - 在 Python 3 中将逗号分隔值列表分解为多个列表的更简单方法

python - matplotlib - 来自矩形高度阵列的 3d 表面

matlab - 如何更改 MatLab 图形的轴限制和刻度步长?

plot - 如何确定 FFT 结果索引 Freq 并绘制幅度/频率图

MatLab:绘制 1/x 时出错

python - 如何通过保护只有经过身份验证的用户才能看到的 url 来使文件私有(private)

python - Mongo集合查询和运算符

python - 如何将每日数据绘制为月平均值(针对不同年份)