How to set axes labels & limits in a Seaborn plot?

In today’s recipe we’ll learn the basics of Seaborn plots customization. The steps below are show using a scatterplot example but for the most they are relevant for any other chart type: barplots, boxplots, heatmaps, lineplots, histplots (histograms) etc’.

We’ll use our simple deliveries dataset. If you are that you can download from here to follow along.

Let’s start by initializing the dataframe:

import pandas as pd
import seaborn as sns

deliveries = pd.read_csv('../../data/del_tips.csv')

Let’s quickly define an sns scatterplot.

scatter, ax = plt.subplots(figsize = (10,7))
ax =sns.scatterplot(x = 'del_tip_amount', y ='time_to_deliver', data=deliveries, hue='type')

Note: We have used the figsize parameter to specify a custom plot size for our scatter.

Obviously, we need to customize the chart to increase readability.

Step 1: Set chart axes labels in Seaborn

Let’s start by defining labels for the axes instead of those being provided by default. We’ll also increase the font size.

ax.set_ylabel('Delivery Time (min)', fontsize = 15)
ax.set_xlabel ('Tip Amount ($)', fontsize = 15)

Step 2: Set sns plot titles

Next we’ll define a title for the chart itself. Here’s the very simple code to use:

ax.set_title('Tips by delivery time)', fontsize = 18)

Step3: Define Seaborn axes limits

We might as well like to modify the axes limits to focus on some outlier results.

ax.set_xlim(left=14, right=20)
ax.set_ylim(bottom=25, top=40)

Note: Here’s a more elaborated Seaborn axis rages setting example.

Step 4: Setting chart legend locations

As you can see above, the lengend is not optimally located in the chart. For completeness, here’s a thorough tutorial on set your legend position in a Seaborn plot.

Leave a Comment