How to add labels to a boxplot figure (pylab)

mkpappu picture mkpappu · Aug 5, 2015 · Viewed 38.1k times · Source

This is a pretty basic question I'm sure but I cannot seem to find the right code. There is my code for the boxplot I am creating. I would like to label the axes and have a title.

from pylab import *
import numpy
raw_data = list(numpy.genfromtxt(filename, delimiter=','))
print raw_data
figure()
boxplot(raw_data,1)
savefig('testfigure.pdf')

I have tried pylab.xlabel('x') and plt.xlable('x') but those do not work...? Do they not work for boxplots or have I just got it wrong about those lines working?

Answer

The Brofessor picture The Brofessor · Aug 5, 2015

Try this:

import matplotlib.pyplot as plt
from pylab import *

# fake up some data
spread= rand(50) * 100
center = ones(25) * 50
flier_high = rand(10) * 100 + 100
flier_low = rand(10) * -100
data =concatenate((spread, center, flier_high, flier_low), 0)

# figure related code
fig = plt.figure()
fig.suptitle('bold figure suptitle', fontsize=14, fontweight='bold')

ax = fig.add_subplot(111)
ax.boxplot(data)

ax.set_title('axes title')
ax.set_xlabel('xlabel')
ax.set_ylabel('ylabel')

plt.show()

EDIT: Picture

enter image description here