11 votes

Tracer des points isolés sur un graphique

J'ai une parcelle de violon qui ressemble à ceci :

enter image description here

Je veux tracer quelques points individuels (ou lignes, croix, points, selon ce qui est le plus facile) sur chaque valeur x, au-dessus des violons, comme ceci :

enter image description here

Comment procéder ?

Il s'agit du code permettant de réaliser les diagrammes de violon (voir Tracé du violon avec 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")

19voto

David Zwicker Points 4319

Il suffit de tracer les données supplémentaires juste après l'autre tracé :

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")

ce qui donne

augmented picture

Prograide.com

Prograide est une communauté de développeurs qui cherche à élargir la connaissance de la programmation au-delà de l'anglais.
Pour cela nous avons les plus grands doutes résolus en français et vous pouvez aussi poser vos propres questions ou résoudre celles des autres.

Powered by:

X