close

How to add labels to a boxplot figure (pylab)

Hello Guys, How are you all? Hope You all Are Fine. Today We Are Going To learn about How to add labels to a boxplot figure (pylab) in Python. So Here I am Explain to you all the possible Methods here.

Without wasting your time, Let’s start This Article.

Table of Contents

How to add labels to a boxplot figure (pylab)?

  1. How to add labels to a boxplot figure (pylab)?

    I would recommend explicitly defining your figure window and plot.
    from pylab import * import numpy as np

  2. add labels to a boxplot figure (pylab)

    I would recommend explicitly defining your figure window and plot.
    from pylab import * import numpy as np

Method 1

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

Method 2

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)

Conclusion

It’s all About this issue. Hope all Methods helped you a lot. Comment below Your thoughts and your queries. Also, Comment below which Method worked for you? Thank You.

Also, Read