简体   繁体   English

具有非规范拟合线的直方图,matplotlib

[英]Histogram with non-normalized fit line, matplotlib

I'm trying to get a set of histograms plotted, with raw count data (non-normalized to density/pdf) and a fit line. 我正在尝试绘制一组直方图,其中包括原始计数数据(未归一化为密度/ pdf)和拟合线。 However, I can't seem to figure out how to get a fit line plotted that ISN'T normalized by a pdf function. 但是,我似乎无法弄清楚如何获得一条由pdf函数标准化的拟合线。 Is there a way to plot a non-normalized line, or a function to reverse the density calculation? 有没有办法绘制非归一化的线,或者有一个函数来反转密度计算? Right now, I've got the below code, which works for the normalized histogram and fit line. 现在,我得到了以下代码,该代码适用于标准化的直方图和拟合线。

fig, ax = plt.subplots()
x=[13.140,17.520,15.768,10.512,10.512,9.636,10.512, 9.636,11.388,7.884,7.008,7.008,9.636,11.388,7.884,7.88,16.64‌​4,42.924,17.520]

n, bins, patches = plt.hist(x, bins=10, normed=False, color='cornflowerblue', alpha=0.75)
(mu, sigma) = norm.fit(x)
y = mlab.normpdf(bins, mu, sigma)
l = plt.plot(bins, y, '-o', linewidth=2)
ax.set_xlabel('Millirems')

This is the graph i have so far, with raw count data and a normalized fit line 这是我到目前为止的图表,其中包含原始计数数据和归一化拟合线 这是我到目前为止的图形,其中包含原始计数数据和归一化拟合线

You could just do this by multiplying the pdf by the total area of the histogram I think? 您可以通过将pdf乘以我认为的直方图的总面积来做到这一点?

import numpy as np

l = plt.plot(bins, y * np.sum(np.diff(bins) * n))

Maybe you want to scale the pdf by the same factor the histogram is scaled with respect to a normed one. 也许您希望按与直方图相对于标准归一化比例相同的因子来缩放pdf。 This factor would be the area of the histogram sum(n * np.diff(bins)) . 该因子将是直方图sum(n * np.diff(bins))的面积。

fig, ax = plt.subplots()
x = [13.140,17.520,15.768,10.512,10.512,9.636,10.512, 9.636,11.388,7.884,7.008,7.008,9.636,11.388,7.884,7.88,16.644,42.924,17.520]

n, bins, patches = plt.hist(x, bins=10, normed=False, color='cornflowerblue', alpha=0.75)
(mu, sigma) = norm.fit(x)
y = mlab.normpdf(bins, mu, sigma) * sum(n * np.diff(bins))
plt.plot(bins, y, '-o', linewidth=2)
ax.set_xlabel('Millirems')

直方图

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM