这个想法可以是获取平均线的坐标并在这些坐标处绘制散点图。
获取坐标可以
要么通过循环平均线的路径来完成,
# loop over the paths of the mean lines
xy = [[l.vertices[:,0].mean(),l.vertices[0,1]] for l in r['cmeans'].get_paths()]
xy = np.array(xy)
或通过重新计算输入数据的平均值。
#alternatively get the means from the data
y = data.mean(axis=0)
x = np.arange(1,len(y)+1)
xy=np.c_[x,y]
完整代码:
import matplotlib.pyplot as plt
import numpy as np; np.random.seed(1)
data = np.random.normal(size=(50, 2))
fig,ax = plt.subplots()
r=ax.violinplot(data,showmeans=True)
# loop over the paths of the mean lines
xy = [[l.vertices[:,0].mean(),l.vertices[0,1]] for l in r['cmeans'].get_paths()]
xy = np.array(xy)
##alternatively get the means from the data
#y = data.mean(axis=0)
#x = np.arange(1,len(y)+1)
#xy=np.c_[x,y]
ax.scatter(xy[:,0], xy[:,1],s=121, c="crimson", marker="o", zorder=3)
# make lines invisible
r['cmeans'].set_visible(False)
plt.show()