matplotlib violin plot 奇怪的输入要求

matplotlib violin plot weird input requirement

为什么 matplotlib 中的小提琴图需要非标准输入?

最小非工作示例

import numpy as np
import matplotlib.pyplot as plt
fig, ax = plt.subplots(3, 1)
ax[0].plot([3, 4, 5])
ax[1].boxplot([3, 4, 5])
ax[2].violin([3, 4, 5])

生成前两个图,但对第三个图给出错误:

TypeError: 'int' object is not subscriptable

以下命令均报错

ax[2].violin([[3, 4, 5]])
ax[2].violin([[3], [4], [5]])
ax[2].violin(np.array([[3, 4, 5]]))
ax[2].violin(np.array([3, 4, 5]))
ax[2].violin([np.array([3, 4, 5])])
ax[2].violin([np.array([[3, 4, 5]])])
ax[2].violin([np.array([[3], [4], [5]])])

文档简单地指出:

dataset : Array or a sequence of vectors.
    The input data.

我必须为此函数输入什么格式,为什么不接受标准数据向量?

ax.violin 不同于 plt.violinplot。来自 ax.violin 的(实际)文档:

ax.violin(vpstats, positions=None, vert=True, widths=0.5, showmeans=False, showextrema=True, showmedians=False)

(...)

vpstats : list of dicts
  A list of dictionaries containing stats for each violin plot.

https://matplotlib.org/api/_as_gen/matplotlib.axes.Axes.violin.html