3

稍微修改一下这里的代码:http: //pyinsci.blogspot.com/2009/09/violin-plot-with-matplotlib.html如下,我可以得到一个用 Python 制作的小提琴图,如下所示:

# Import modules
import pylab as pl
from scipy import stats
import numpy as np

# Function for Violin Plot

def violin_plot(ax,data,groups,bp=False):
    '''Create violin plot along an axis'''
    dist = max(groups) - min(groups)
    w = min(0.15*max(dist,1.0),0.5)
    for d,p in zip(data,groups):
        k = stats.gaussian_kde(d) #calculates the kernel density
        m = k.dataset.min() #lower bound of violin
        M = k.dataset.max() #upper bound of violin
        x = np.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)

groups = range(3)
a = np.random.normal(size=100)
b = np.random.normal(size=100)
c = np.random.normal(size=100)

data = np.vstack((a,b,c))
fig = pl.figure()
ax = fig.add_subplot(111)
violin_plot(ax,data,groups,bp=0)
pl.show()

这会产生一个像这样的图形:

在此处输入图像描述

我想做的是更改刻度线的标签,这样就不是-0.5到2.5乘0.5个数字,只有一个“A”,0.0是,一个“B”是1.0,一个“C”是2.0。

是否有捷径可寻?

4

1 回答 1

4

在调用之前pl.show,请使用:

ax.set_xticks([0, 1, 2])
ax.set_xticklabels(['A', 'B', 'C'])
于 2013-02-26T05:39:31.770 回答