Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Changing Axis Labels for a Box/Violin Plot in Matplotlib

Tags:

matplotlib

Slightly adapting the code from here: http://pyinsci.blogspot.com/2009/09/violin-plot-with-matplotlib.html as follows, I can get a violin plot produced in Python as follows:

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

This produces a figure like so:

enter image description here

What I would like to do is change the labeling of the tick marks, so that rather than -0.5 to 2.5 by 0.5 numbers, there's just an "A" where 0.0 would be, a "B" at 1.0 and a "C" at 2.0.

Is there an easy way to do this?

like image 280
Fomite Avatar asked Dec 03 '25 18:12

Fomite


1 Answers

Before you're call to pl.show, use:

ax.set_xticks([0, 1, 2])
ax.set_xticklabels(['A', 'B', 'C'])
like image 94
Paul H Avatar answered Dec 06 '25 10:12

Paul H



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!