How to display text on boxplot in Python?
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.
The shape of the dataframe is: (20, 3)
Printing the first five rows in the dataset:
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.
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