Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to add labels to a boxplot figure (pylab)

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?

like image 712
mkpappu Avatar asked Aug 05 '15 21:08

mkpappu


People also ask

How do you add labels to box plots?

Adding Labels We can add labels using the xlab,ylab parameters in the boxplot() function. By using the main parameter, we can add heading to the plot. Notch parameter is used to make the plot more understandable.

Do boxplots have labels?

A box graph is a chart that is used to display information in the form of distribution by drawing boxplots for each of them. Boxplots help us to visualize the distribution of the data by quartile and detect the presence of outliers. Adding axis labels for Boxplot will help the readability of the boxplot.

What do you label boxplots with?

The five-number summary is the minimum, first quartile, median, third quartile, and maximum. In a box plot, we draw a box from the first quartile to the third quartile. A vertical line goes through the box at the median.


2 Answers

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

like image 188
The Brofessor Avatar answered Sep 28 '22 23:09

The Brofessor


I would recommend explicitly defining your figure window and plot.

from pylab import *
import numpy as np

fig = figure(figsize=(4,4))  # define the figure window
ax  = fig.add_subplot(111)   # define the axis

ax.boxplot(raw_data,1)       # make your boxplot

# add axis texts
ax.set_xlabel('X-label', fontsize=8)
ax.set_ylabel('Y-label', fontsize=8)
ax.set_title('I AM BOXPLOT', fontsize=10)

# format axes
ax.set_xlim([0,100])
ax.set_xticks( np.arange(0,101,10), minor=False)
ax.set_xticks( np.arange(0,100,5),  minor=True)

# if you wish to explicitly set tick labels
ax.set_xticklabels( np.arange(0,101,10), fontsize=8)

# if you wish to explicitly set actual tick parameters
ax.tick_params(axis='both',which='major',direction='in',length=4,width=2,labelsize=8)
ax.tick_params(axis='both',which='minor',direction='in',length=2,width=1.5)  

# and so on...you can do the same for the y-axis.  
# You have quite a lot of control over the axes this way.

another tip, when saving set bbox_inches to 'tight' so you don't cut off your labels

savefig('fig_title.jpg', bbox_inches='tight', dpi=500)
like image 37
tnknepp Avatar answered Sep 29 '22 01:09

tnknepp