Python 使用matplotlib在同一帧上绘制曲线和直方图

Python 使用matplotlib在同一帧上绘制曲线和直方图,python,python-3.x,matplotlib,plot,Python,Python 3.x,Matplotlib,Plot,我希望曲线和直方图与matplotlib在同一个绘图上共存。曲线是法线曲线,直方图由数据集组成。我想比较我的样本的直方图和曲线,如果我有大量的数据,我的样本的重新划分应该是什么。目的是检查是否存在危险以外的其他因素 代码如下: def testHistogram(arr, mean, variance): from matplotlib import pyplot as plt import numpy as np import scipy.stats as stats

我希望曲线和直方图与matplotlib在同一个绘图上共存。曲线是法线曲线,直方图由数据集组成。我想比较我的样本的直方图和曲线,如果我有大量的数据,我的样本的重新划分应该是什么。目的是检查是否存在危险以外的其他因素

代码如下:

def testHistogram(arr, mean, variance):
    from matplotlib import pyplot as plt
    import numpy as np
    import scipy.stats as stats
    import math

    # histogram
    num_bins = 100
    plt.hist(arr, num_bins, facecolor='yellow', alpha=0.5)

    # plot
    mu = mean
    sigma = math.sqrt(variance)
    x = np.linspace(mu - 3*sigma, mu + 3*sigma, 100)
    plt.plot(x, stats.norm.pdf(x, mu, sigma))

    plt.grid(True)
    plt.show()
我的问题是,曲线没有出现。直方图似乎工作正常

参数:

#create a dataset for testing - i am getting it from a database 
import random
myList = []
while (i<100):
    randomnumber = random.randint(1,100)
    myList.append(randomnumber)
    i = i+1
#get the mean and variance of the dataset
count = 0
sum = 0
squaresSum = 0
theMean = 0
for onedata in dataset:
    count = count+1
    sum = sum + onedata
    squaressum = squaresSum + onedata*onedata
theMean = sum/count
theVariance = squaresSum/count - theMean*theMean

# launch the function
testHistogram(myList, theMean, theVariance)
arr:要与曲线进行比较的样本值列表 平均值:样本的平均值,以便建立相应的曲线 方差:样本的方差,用于构建相应的曲线 编辑:如评论中所述,以下是如何创建参数:

#create a dataset for testing - i am getting it from a database 
import random
myList = []
while (i<100):
    randomnumber = random.randint(1,100)
    myList.append(randomnumber)
    i = i+1
#get the mean and variance of the dataset
count = 0
sum = 0
squaresSum = 0
theMean = 0
for onedata in dataset:
    count = count+1
    sum = sum + onedata
    squaressum = squaresSum + onedata*onedata
theMean = sum/count
theVariance = squaresSum/count - theMean*theMean

# launch the function
testHistogram(myList, theMean, theVariance)

实际上,您的代码几乎可以正常工作,您只需要规范化直方图。返回规格化曲线,即曲线的积分为1。 在你的例子中,你可能有一条非常低的曲线,在x轴上几乎是平坦的,你看不见

要规范化直方图,只需将参数density=True传递给hist函数:

plt.hist(arr, num_bins, density=True, facecolor='yellow', alpha=0.5)
例如,以下代码是您的一个小修改:

from matplotlib import pyplot as plt
import numpy as np
import scipy.stats as stats
import math

def testHistogram(arr, mean, variance):
    # histogram
    num_bins = 100
    plt.hist(arr, num_bins, density=True, facecolor='yellow', alpha=0.5)

    # plot
    mu = mean
    sigma = math.sqrt(variance)
    x = np.linspace(mu - 3*sigma, mu + 3*sigma, 100)
    plt.plot(x, stats.norm.pdf(x, mu, sigma))

    plt.grid(True)
    plt.show()


mm = 100  #norm mean valu
vv = 40   #norm variance
#x is an array of 100 random numbers from the normal distribution
x = np.random.normal(mm, math.sqrt(vv), 100)
testHistogram(x, mm, vv)
绘制下图:


你的代码不起作用。你能提供一个能工作的代码吗?我们可以简单地复制你的身材?由于您似乎对堆栈溢出还不熟悉,所以您应该阅读很多感谢,这正是我做错的地方,而您的更正正是我所需要的。