Adding Annotations to Seaborn Plots

...insert text and numbers

Seaborn is a plotting library which provides us with plenty of options to visualize our data analysis. Based on matplotlib, seaborn enables us to quickly generate a neat and sleek visualization with sensible defaults with a single line of code. Most of our visualization needs during Exploratory Data Analysis (EDA) are adequately and easily handled by seaborn. However, one aspect which one may have found wanting with these plots is to be able to annotate these plots with values and other highlights which would more effectively bring out the story or insight you are trying to extract from the data.

Below, I will show you a couple of ways in which you can add annotations (eg. value of median in a box plot or the value corresponding to a barplot) to your plots.First up, let us load the required libraries.

We will use the in-built tips dataset from seabornThe tips dataset has 244 observations of 7 features that include the total bill amount, tip amount, gender of the person paying the bill, whether he/she is a smoker or not, day on which the meal was had, time (dinner/lunch) and size of the group having the meal.Now, say we want to check out the distribution of the total bill value by day of the week to see if there is any difference. It is easily done using the boxplots for each day of the week.But wouldn't it be great if we could also display the median value of total bill on each day of the week? Yes, that would be nice and we can do it! First, let us calculate the median total bill amount day-wise using the groupby( ) function.

The annotate function of the plot object comes to our rescue for inserting these values into the plot. It takes 2 parameters — the text to be displayed as a string data type and the xy argument through which you can specify the location of the text as a tuple for the x and y coordinates. You may have to play around with the y coordinate to work out the best possible alignment for your plot. Since we have a categorical feature day along the x axis, the category labels (thu— sun) correspond to the x coordinate of 0 to 3.

Similarly, you can add Q1, Q3 values if you prefer.Another way to do this is using the text function of the plot object. To demonstrate this, we will now plot the same two features (day & total bill) as a bar plot. First, let's plot the plain vanilla bar plot which shows us the mean value of the total bill amount for each day.

Now, we will add the annotation (the mean value of the total bill amount) using the text function. This function takes in similar parameters like annotate to do the job. First is the x coordinate, then y and then the string to display at the specified location. Other font-related aesthetics settings can be passed as a dictionary to the fontdict parameter of the function.

If you want the mean bill amount to be shown above each bar instead of within, just replace means[i]/2 by means[i] + 0.25 (an offset so that the value is displayed above the bar and doesn't clash with it).

There, that was not too difficult, was it ? Hope, you liked this article. Stay tuned for more informative blogs on python. Ensure to check and renew your membership to continue to get unlimited access to the blog content.

Mon Feb 13, 2023

Say Yes to New Adventures

"Lorem Ipsum has been the industry's standard dummy text ever since the 1500s." — James Chapman

Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. Lorem ipsum dolor amet, consectetur adipiscing elit.

Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book.

Balaji Sundararaman
A California-based travel writer, lover of food, oceans, and nature.