How to add a standard normal pdf over a seaborn histogram(如何在海运直方图上添加标准普通pdf)
本文介绍了如何在海运直方图上添加标准普通pdf的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想在用seaborn构建的直方图上添加标准的普通pdf曲线。
import numpy as np
import seaborn as sns
x = np.random.standard_normal(1000)
sns.distplot(x, kde = False)
如有任何帮助,我们将不胜感激!
推荐答案
scipy.stats.norm使用
可以轻松访问正态分布的pdf 已知参数;默认情况下,它对应于标准法线,mu=0,sigma=1。- 无论数据平均值位于何处(例如
mu=0或mu=10),此答案都适用。
- 无论数据平均值位于何处(例如
- 测试于
python 3.8.11、matplotlib 3.4.2、seaborn 0.11.2 - 本问答适用于轴级图;图级图请参见How to draw a normal curve on seaborn displot
导入和数据
import numpy as np
import seaborn as sns
from scipy import stats
import matplotlib.pyplot as plt
# data
np.random.seed(365)
x = np.random.standard_normal(1000)
seaborn.histplot
ax = sns.histplot(x, kde=False, stat='density', label='samples')
# calculate the pdf
x0, x1 = ax.get_xlim() # extract the endpoints for the x-axis
x_pdf = np.linspace(x0, x1, 100)
y_pdf = scipy.stats.norm.pdf(x_pdf)
ax.plot(x_pdf, y_pdf, 'r', lw=2, label='pdf')
ax.legend()
seaborn.distplot-已弃用
- 为使其与您的采样数据正确对应,直方图应
显示密度而不计数,因此在seaborn.distplot调用中使用norm_hist=True。
ax = sns.distplot(x, kde = False, norm_hist=True, hist_kws={'ec': 'k'}, label='samples')
# calculate the pdf
x0, x1 = ax.get_xlim() # extract the endpoints for the x-axis
x_pdf = np.linspace(x0, x1, 100)
y_pdf = scipy.stats.norm.pdf(x_pdf)
ax.plot(x_pdf, y_pdf, 'r', lw=2, label='pdf')
ax.legend()
这篇关于如何在海运直方图上添加标准普通pdf的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
织梦狗教程
本文标题为:如何在海运直方图上添加标准普通pdf
基础教程推荐
猜你喜欢
- kivy 应用程序中的一个简单网页作为小部件 2022-01-01
- Kivy 使用 opencv.调整图像大小 2022-01-01
- 比较两个文本文件以找出差异并将它们输出到新的文本文件 2022-01-01
- 究竟什么是“容器"?在蟒蛇?(以及所有的 python 容器类型是什么?) 2022-01-01
- Python 中是否有任何支持将长字符串转储为块文字或折叠块的 yaml 库? 2022-01-01
- 对多索引数据帧的列进行排序 2022-01-01
- Python,确定字符串是否应转换为 Int 或 Float 2022-01-01
- 在 Python 中将货币解析为数字 2022-01-01
- matplotlib 设置 yaxis 标签大小 2022-01-01
- 在 Django Admin 中使用内联 OneToOneField 2022-01-01
