Skip to content
Related Articles

Related Articles

Improve Article

How to display text on boxplot in Python?

  • Last Updated : 21 Jul, 2021

Boxplot is an important graphical plot that can be used to get a summary of data present in numerical form. The plot can give us information about statistical measures such as percentile, median, minimum and maximum values of the numerical data. In the box plot, the line which passes through the center of the box represents the median value. In this article, we will be learning about how to add plot a boxplot and display text on the boxplot.

Firstly, to create boxplot we’ll use the seaborn library, and to display text on the boxplot we will be using the text() method available inside the matplotlib.pyplot class. The text() method enables us to write strings on the plots and add customizations to them.

Let’s plot a simple boxplot at first and add some text over it.


# import modules
import numpy as np
import pandas as pd
import seaborn as sns
import matplotlib.pyplot as plt'seaborn')
# Reading the dataset
data = pd.read_csv('Dataset.csv')
print("The shape of the dataframe is: ",data.shape)


The shape of the dataframe is:  (20, 3)

Printing the first five rows in the dataset:


# display data






# depict box plot visualization
plt.figure(figsize=(10, 6))
sns.boxplot(data['Width (in cm)'])
plt.text(75, 0.07, 'Outliers beyond beyond 75% value', fontsize=14)





We can observe from the above-written code, that plt.text() method was used to display the desired text that we want. It requires three compulsory positional arguments:


Syntax: plt.text(x, y, text) 


  • x-coordinate: denotes the location of the text on x-axis
  • y-coordinate: denotes the location of text on y-axis
  • text: denotes the string that we want to insert.


To make our box plot look more appealing we can pass many other optional parameters for formatting the text which has been discussed in the next plot.


# add text
plt.figure(figsize=(10, 6))
sns.boxplot(data['Height (in cm)'])
plt.text(3, 0.07,
         'Points beyond 25% value',
plt.text(95, 0.07,
         'Points beyond beyond 75% value',


As can be observed from the plot we have added a nice bounding box around our text and have also added colours onto our text that can be done by passing the optional parameters inside plt.text() method such as:

  • bbox: used to create a bounding box around the text which itself accepts a dictionary for adding color to the box and setting the opacity of color.
  • fontsize: used to set the size of our font.
  • horizontalalignment or ha: to set the alignment of the text horizontally.
  • verticalalignment or va: to set the alignment of the text vertically.

And there are many others as well that can be seen from the official documentation of the method.

 Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.  

To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course. And to begin with your Machine Learning Journey, join the Machine Learning – Basic Level Course

My Personal Notes arrow_drop_up
Recommended Articles
Page :