当 matplotlib 的 violinplot 中的 points 参数发生变化时会发生什么?何时这样做有用?
points参数定义如下。
points : scalar, default = 100
Defines the number of points to evaluate each of the gaussian kernel density estimations at.
Axes.violinplot(self, dataset, positions=None, vert=True, widths=0.5, showmeans=False, showextrema=True, showmedians=False, points=100, bw_method=None, *, data=None)
当我改变图表时,我发现图表上的变化很小。为什么会这样?
请您参考如下方法:
根据[官方文档](我的第二个重点)(https://matplotlib.org/3.1.0/api/_as_gen/matplotlib.axes.Axes.violinplot.html)
points : scalar, default = 100 Defines the number of points to evaluate each of the gaussian kernel density estimations at.
So, as the following example (adapted from here ) demonstrates, the effect of number of points is highlighted when chosen a very small number of points.结果当然也取决于样本量。尝试选择较小的样本大小,如 size=5 并运行下面的相同代码。随着点数的增加,密度估计的平滑度自然会提高。在接受收敛测试的某些截止点数处,您将看不到明显的影响。
import numpy as np
import matplotlib.pyplot as plt
# Fixing random state for reproducibility
np.random.seed(19680801)
fs = 10 # fontsize
pos = [1, 2, 4, 5, 7, 8]
data = [np.random.normal(0, std, size=100) for std in pos]
fig, axes = plt.subplots(nrows=1, ncols=3, figsize=(10, 3))
axes[0].violinplot(data, pos, points=2, widths=1,
showmeans=True, showextrema=True, showmedians=True)
axes[0].set_title('Custom violinplot 1', fontsize=fs)
axes[1].violinplot(data, pos, points=5, widths=1,
showmeans=True, showextrema=True, showmedians=True,
bw_method='silverman')
axes[1].set_title('Custom violinplot 2', fontsize=fs)
axes[2].violinplot(data, pos, points=100, widths=1,
showmeans=True, showextrema=True, showmedians=True,
bw_method='silverman')
axes[2].set_title('Custom violinplot 2', fontsize=fs)
for ax in axes.flat:
ax.set_yticklabels([])
plt.tight_layout()
P.S:为了进一步强调这一点,只考虑一个位置和三种情况:@ImportanceOfBeingEarnest 建议的 5、10 和 50 点
pos = [1]
data = [np.random.normal(0, std, size=100) for std in pos]
