pandas 给定均值和西格玛绘制正态分布-python

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/31674195/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-13 23:41:23  来源:igfitidea点击:

plot normal distribution given mean and sigma - python

pythonpandasmatplotlibplotseaborn

提问by tomtom

I have some data in pandas dataframe

我在Pandas数据框中有一些数据

df['Difference'] = df.Congruent.values - df.Incongruent.values
mean = df.Difference.mean()
std = df.Difference.std(ddof=1)
median = df.Difference.median()
mode = df.Difference.mode()

and I want to plot a histogram together with normal distribution in 1 plot. Is there a plotting function that takes mean and sigma as arguments? I don't care whether it is matplotplib, seaborn or ggplot. The best would be if I could mark also mode and median of the data all within 1 plot.

我想在 1 个图中绘制直方图和正态分布。是否有以均值和西格玛为参数的绘图函数?我不在乎它是 matplotplib、seaborn 还是 ggplot。最好的是,如果我也可以在 1 个图中标记所有数据的众数和中位数。

回答by xnx

You can use matplotlib/pylab with scipy.stats.norm.pdfand pass the mean and standard deviation as locand scale:

您可以使用 matplotlib/pylabscipy.stats.norm.pdf并将平均值和标准偏差作为locand传递scale

import pylab
import numpy as np
from scipy.stats import norm
x = np.linspace(-10,10,1000)
y = norm.pdf(x, loc=2.5, scale=1.5)    # for example
pylab.plot(x,y)
pylab.show()

enter image description here

在此处输入图片说明