How can I change the axis labels on a Seaborn plot?

To change the axis labels on a Seaborn plot, one must use the “set_xlabel” and “set_ylabel” functions within the “plt” module. These functions allow the user to specify the desired labels for the x and y axes respectively. The new labels should be defined as strings and passed as arguments within the functions. This will update the labels on the plot accordingly. Additionally, the “plt.title” function can also be used to change the title of the plot. By utilizing these functions, one can easily customize the axis labels on a Seaborn plot to better represent their data.

Change Axis Labels on a Seaborn Plot (With Examples)


There are two ways to change the axis labels on a seaborn plot.

The first way is to use the ax.set() function, which uses the following syntax:

ax.set(xlabel='x-axis label', ylabel='y-axis label')

The second way is to use matplotlib functions, which use the following syntax:

plt.xlabel('x-axis label')
plt.ylabel('y-axis label')

The following examples show how to use each of these methods in practice.

Method 1: Change Axis Labels Using ax.set()

The following code shows how to create a seaborn barplot and use ax.set() to specify the axis labels:

import pandas as pd
import seaborn as sns
import matplotlib.pyplotas plt

#create some fake data
df = pd.DataFrame({'quarter': ['Q1', 'Q2', 'Q3', 'Q4'],
                   'sales': [23, 26, 24, 34]})

#create seaborn barplot
ax = sns.barplot(x='quarter', y='sales', 
                 data = df, 
                 color='steelblue')

#specfiy axis labels
ax.set(xlabel='Sales Quarter',
       ylabel='Total Sales',
       title='Sales by Quarter')

#display barplot
plt.show()

Method 2: Change Axis Labels Using Matplotlib Functions

The following code shows how to create a seaborn barplot and use matplotlib functions to specify the axis labels:

import pandas as pd
import seaborn as sns
import matplotlib.pyplotas plt

#create some fake data
df = pd.DataFrame({'quarter': ['Q1', 'Q2', 'Q3', 'Q4'],
                   'sales': [23, 26, 24, 34]})

#create seaborn barplot
ax = sns.barplot(x='quarter', y='sales', 
                 data = df, 
                 color='steelblue')

#specify axis labels
plt.xlabel('Sales Quarter')
plt.ylabel('Total Sales')
plt.title('Sales by Quarter')

#display barplot
plt.show()

Note that you can also specify the font size, font style, font family, and other font features using this method:

#specify axis labels
plt.xlabel('Sales Quarter', size=16, fontstyle='italic', weight=900)
plt.ylabel('Total Sales', size=16, family='monospace')
plt.title('Sales by Quarter')

#display barplot
plt.show()

x