本文介绍了用频率计数绘制概率密度函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想将拟合分布转换为频率。

I want to convert fitted distribution to frequency.

import numpy as np
import matplotlib.pyplot as plt
from scipy import stats
%matplotlib notebook

# sample data generation
np.random.seed(42)
data = sorted(stats.lognorm.rvs(s=0.5, loc=1, scale=1000, size=1000))

# fit lognormal distribution
shape, loc, scale = stats.lognorm.fit(data, loc=0)
pdf_lognorm = stats.lognorm.pdf(data, shape, loc, scale)

fig, ax = plt.subplots(figsize=(8, 4))

ax.hist(data, bins='auto', density=True)
ax.plot(data, pdf_lognorm)
ax.set_ylabel('probability')
ax.set_title('Linear Scale')

上面的代码段将生成以下图:

The above code snippet will generate the following plot:

如您所见,y轴是概率。但是我希望它以频率来表示。

As you can see, the y-axis is in terms of probability. But I want it to be in terms of frequencies.

fig, ax = plt.subplots(figsize=(8, 4))
ax.hist(data, bins='auto')
ax.set_ylabel('probability')
ax.set_title('Linear Scale')

通过取消设置 density = True ,可以按频率显示直方图。但是我不知道如何像直方图中那样拟合分布-请观察我如何无法在此直方图中绘制橙色拟合线。

By unsetting density=True, the histogram is displayed in terms of frequencies. But I do not know how to fit the distribution in the same way as it is in histograms - observe how I couldn't draw the orange fitted line in this histogram.

我该怎么做?我想我应该将拟合分布与直方图曲线下的面积相乘,但是我不知道该怎么做。

How can I do this? I think I should multiply the fitted distribution with the area under the curve of the histogram, but I don't know how to.

推荐答案

从科学上讲,确实可以预期,因为您决定还绘制密度图,因此y轴将是概率,而不是计数...

Scientifically speaking, it is indeed expected that, since you decide to also plot the density, the y-axis will be in probability, and not in counts...

不过,您可以同时使用双轴和 twinx

Nevertheless, you can have both using dual axes and twinx:

fig, ax = plt.subplots(figsize=(8, 4))
ax2 = ax.twinx()

ax.hist(data, bins='auto', density=True)
ax2.hist(data, bins='auto')
ax.plot(data, pdf_lognorm)
ax2.set_ylabel('frequency')
ax.set_ylabel('probability')
ax.set_title('Linear Scale')][1]][1]

其中,我还使用了更合适的术语频率

where I have also used the more appropriate term 'frequency' for the counts.

进行一点实验,您甚至可以将密度曲线置于前面,或者互换轴:

Experimenting a little you may even bring the density curve in the front, or interchange the axes:

fig, ax = plt.subplots(figsize=(8, 4))
ax2 = ax.twinx()

ax2.hist(data, bins='auto', density=True)
ax.hist(data, bins='auto')
ax2.plot(data, pdf_lognorm)
ax2.set_ylabel('probability')
ax.set_ylabel('frequency')
ax.set_title('Linear Scale')

这篇关于用频率计数绘制概率密度函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-20 09:44