擅长:python、mysql、java
<p>如果要绘制分布,并且您知道它,请将其定义为函数,并按此方式绘制:</p>
<pre><code>import numpy as np
from matplotlib import pyplot as plt
def my_dist(x):
return np.exp(-x ** 2)
x = np.arange(-100, 100)
p = my_dist(x)
plt.plot(x, p)
plt.show()
</code></pre>
<hr/>
<p>如果没有作为分析函数的精确分布,也许可以生成一个大样本,提取直方图并以某种方式平滑数据:</p>
<pre><code>import numpy as np
from scipy.interpolate import UnivariateSpline
from matplotlib import pyplot as plt
N = 1000
n = N//10
s = np.random.normal(size=N) # generate your data sample with N elements
p, x = np.histogram(s, bins=n) # bin it into n = N//10 bins
x = x[:-1] + (x[1] - x[0])/2 # convert bin edges to centers
f = UnivariateSpline(x, p, s=n)
plt.plot(x, f(x))
plt.show()
</code></pre>
<p>可以在<code>UnivariateSpline</code>函数调用中增加或减少<code>s</code>(平滑因子)来增加或减少平滑。例如,使用这两种方法:
<img src="https://i.stack.imgur.com/2Vngk.png" alt="dist to func"/></p>