2017-05-30 55 views
0

我想改变小提琴情节中平均值的外观。我正在使用matplotlib。我可以改变的手段的颜色与下面的代码:将小提琴情节中的平均指标改为圆圈

import matplotlib.pyplot as plt 

fig,(axes1,axes2,axes3) = plt.subplots(nrows=3,ncols=1,figsize=(10,20)) 

r=axes2.violinplot(D,showmeans=True,showmedians=True) 
r['cmeans'].set_color('red') 

但现在我想改变平均值(目前为一条线,像中值)为“小圈子”的样子。 有人可以帮助我吗?

+0

也许你可以发表你的当前地块的外观。还请添加一些更多的细节,以清楚地了解到底是什么问题。 –

回答

0

这个想法可以获得平均线的坐标并在这些坐标处绘制散点图。

获取的坐标可以

  • 或者通过遍历的平均线路径进行,

  • 或通过从输入数据reacalculating平均值。

    #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() 

enter image description here

+0

非常感谢!第一种方式完美地工作! :) – Leo