我正在使用seaborn进行数据绘制,直到我的指导者问我如何在以下代码中进行绘制为止,一切都很好。
import numpy as np
import pandas as pd
import seaborn as sns
import matplotlib.pyplot as plt
from scipy import stats
x = np.random.normal(size=100)
sns.distplot(x)
此代码的结果是:
1-我想知道,distplot如何绘制此图?
2-为什么情节从-3开始到4结束?
3-是否有distplot用于绘制像这样的数据的任何参数函数或任何特定的数学函数?
这些是我在寻找答案的一些问题,我使用distplot和kde绘制数据,但我想知道这些python函数背后的数学原理。
这里有一些代码试图说明如何绘制kde曲线。
import matplotlib.pyplot as plt
import numpy as np
def gauss(x, mu, sigma):
return np.exp(-((x - mu) / sigma) ** 2 / 2) / (sigma * np.sqrt(2 * np.pi))
N = 100
xs = np.random.normal(0, 1, N)
print('sigma with Scott''s rule', N ** (-1. / 5))
plt.hist(xs, density=True, label='Histogram', alpha=.4, ec='w')
for sigma in np.arange(.2, 1.2, .2):
x = np.linspace(xs.min() - 1, xs.max() + 1, 100)
plt.plot(x, sum(gauss(x, xi, sigma) for xi in xs) / N, label=f'$\\sigma = {sigma:.1f}$')
plt.xlim(x[0], x[-1])
plt.legend()
plt.show()