绘制图表上的单个点

时间:2013-03-13 10:35:48

标签: python matplotlib

我的小提琴情节如下:

enter image description here

我想在每个x值上绘制几个单独的点(或线条,十字架,最简单的点),在小提琴之上,如下所示:

enter image description here

我该怎么做?

这是制作小提琴情节的代码(见Violin Plot with Matplotlib

from matplotlib.pyplot import figure, show
from scipy.stats import gaussian_kde
from numpy.random import normal
from numpy import arange

def violin_plot(ax, data, pos, bp=False):
    '''                                                                                                                                                                                          
    create violin plots on an axis                                                                                                                                                               
    '''
    dist = max(pos)-min(pos)
    w = min(0.15*max(dist,1.0),0.5)
    for d,p in zip(data,pos):
        k = gaussian_kde(d) #calculates the kernel density                                                                                                                                       
        m = k.dataset.min() #lower bound of violin                                                                                                                                               
        M = k.dataset.max() #upper bound of violin                                                                                                                                               
        x = arange(m,M,(M-m)/100.) # support for violin                                                                                                                                          
        v = k.evaluate(x) #violin profile (density curve)                                                                                                                                        
        v = v/v.max()*w #scaling the violin to the available space                                                                                                                               
        ax.fill_betweenx(x,p,v+p,facecolor='y',alpha=0.3)
        ax.fill_betweenx(x,p,-v+p,facecolor='y',alpha=0.3)
    if bp:
        ax.boxplot(data,notch=1,positions=pos,vert=1)

if __name__=="__main__":
    pos = range(5)
    data = [normal(size=100) for i in pos]
    fig=figure()
    ax = fig.add_subplot(111)
    violin_plot(ax,data,pos,bp=1)
    fig.savefig("violins.gif")

1 个答案:

答案 0 :(得分:18)

只需在其他情节之后绘制额外数据:

from matplotlib.pyplot import figure, show
from scipy.stats import gaussian_kde
from numpy.random import normal
from numpy import arange

def violin_plot(ax, data, pos, bp=False):
    '''
    create violin plots on an axis
    '''
    dist = max(pos)-min(pos)
    w = min(0.15*max(dist,1.0),0.5)
    for d,p in zip(data,pos):
        k = gaussian_kde(d) #calculates the kernel density
        m = k.dataset.min() #lower bound of violin
        M = k.dataset.max() #upper bound of violin
        x = arange(m,M,(M-m)/100.) # support for violin
        v = k.evaluate(x) #violin profile (density curve)
        v = v/v.max()*w #scaling the violin to the available space
        ax.fill_betweenx(x,p,v+p,facecolor='y',alpha=0.3)
        ax.fill_betweenx(x,p,-v+p,facecolor='y',alpha=0.3)
    if bp:
        ax.boxplot(data,notch=1,positions=pos,vert=1)

if __name__=="__main__":
    pos = range(5)
    data = [normal(size=100) for i in pos]
    fig=figure()
    ax = fig.add_subplot(111)
    violin_plot(ax,data,pos,bp=1)

    data_x = [0, 1, 2, 2, 3, 4]
    data_y = [1.5, 1., 0.7, 2.5, 1, 1.5]
    ax.plot(data_x, data_y, 'or')
    fig.savefig("violins.gif")

给出了

augmented picture