Python

Seaborn Axis Labels

In this post, we will try to set axes labels and their boundaries in a Seaborn plot. Let’s start with some fundamental ideas.The data space is contained by the axis, which is a region on the plot. The Axes comprise two or three-axis objects (in the case of 3D) that manage the data boundaries. Axes Labels are labels that indicate the significance, dimensions, and orientation of the axes’ values. Axes Limits are the upper and lower bounds on the axes’ data, which are used to search for a certain value on the axes.

Setting the axis labels, axes limitations, and both at the same time are all covered in this article. Finally, you’ll be able to use a Seaborn plot to establish axes labels and boundaries. When we use the plot function to specify values for the x and y axes, the graph uses these values as labels for both axes by default. There are three methods for adding our preferred axis labels that will be discussed here.

Method 1: Set the Function for Axes Labels in Seaborn Plot

Using matplotlib.axes, we can label the axes in the seaborn plot. Python’s matplotlib library has a function called axes.set().

Syntax:

Axes.set(self, xlabel, ylabel, labelpad=None, **kwargs)

The Axes.set takes the xlabel and ylabel arguments which are string labels for the x-axis and the y-axis. The labelpad is the optional argument that has the scalar property and by default values are set to None. The **kwargs are the property for string. Through this method, the labels on the x- and y-axes are changed.

Example:

In this example, we’ll utilize a single call to the matplotlib.axes.Axes.set() function to alter the label of the user plot. We will provide both the xlabel and ylabel arguments in one attempt, and the user plot will be changed.

We added two libraries: matplotlib and the seaborn library for plot generating. Then we set the style of our plot as the darkgrid. The sample dataset “titanic” is loaded inside the variable titanic. The new variable is established as Myplot where the seaborn plot function is utilized and set the parameter variables x and y. Within the Myplot.set function, we set the xlabel as Survived and the ylabel as Age. The title name for the plot is also set as ‘Titanic Dataset’.

import matplotlib.pyplot as plt

import seaborn as sns

sns.set_style("darkgrid")

 

titanic = sns.load_dataset("titanic")

MyPlot = sns.boxplot(x ="survived", y ="age", data = titanic)

 

MyPlot.set(xlabel ="Survived", ylabel = "Age", title ='Titanic Dataset')

plt.show()

The plot visualization with the axes xlabel and ylabel are shown inside the figure along with the title of the plot.

Method 2: Set the Function for Axes Limitations in Seaborn Plot

Using matplotlib.axes, we can label the axes in the seaborn plot: matplotlib.axes and axes.set ylabel(). The matplotlib library’s axes.set xlabel() function is used. The python function axes.set xlabel() comes from the matplotlib module. To modify the x-axis label, use the Axes.set xlabel() method in the axes module of the matplotlib package. To update the y-axis label, use the Axes.set ylabel() method from the axes module of the matplotlib toolkit.

Syntax:

Axes.set_xlabel(self, xlabel,labelpad=None, **kwargs)

Axes.set_ylabel(self, xlabel,labelpad=None, **kwargs)

The xlabel, labelpad, and kwargs parameters are accepted by this method. The xlabel parameter is the string label, and the point spacing from the axis bounding box comprising ticks and tick labels is defined by the labelpad option. Note that there is no value returned by this method.

Example:

To alter the label of the plot, we will use the matplotlib.axes.Axes.set ylabel() and matplotlib.axes.Axes.set xlabel() functions individually and deliver the title of the label as the parameter.

Here, we styled our plot as whitegrid. The sample dataset we chose for generating a plot is the iris. The dataset iris is loaded inside the variable “Iris”. Then, we created the boxplot and set its variable parameters x and y. The function is called Iris_plot.set_tilte which sets the title for the plot as “Iris Dataset Plot”. Likewise, the function for the xlabel and ylabel axes set the label which is specified inside the function.

import matplotlib.pyplot as plt

import seaborn as sns

sns.set_style("whitegrid")

 

Iris = sns.load_dataset("iris")

 

Iris_plot = sns.boxplot(x ="petal_length", y ="petal_width", data =Iris)

Iris_plot.set_title( "Iris Dataset plot")

 

Iris_plot.set_xlabel( "Iris petal_length")

Iris_plot.set_ylabel( "Iris petal_width")

plt.show()

The plot representation with the label for xlabel and ylabel axes, and the title for the plot is as follows:

Method 3: Set the Function for Both Axes Labels and Limitations in Seaborn Plot

Limits can be changed using the following functions:

matplotlib.axes.Axes.set xlim(): To modify the x-axis view boundaries, use the axes module from the matplotlib package.

matplotlib.axes.Axes.set_ylim(): The y-axis view boundaries are configured using the matplotlib library’s axes module.

Syntax:

Axes.set_xlim(self, left=None, right=None, emit=True, xmin=None, xmax=None)

Axes.set_ylim(self, bottom=None, top=None, emit=True, ymin=None, ymax=None)

Above are the parameters that these functions take which include the following:

bottom: In data coordinates, this value is the lowest xlim/ylim.

top: In data coordinates, this value represents the top xlim/ylim.

emit: This option is used to alert observers of a change in the limit.

xmin, xmax, ymin, ymax: The updated x-axis/y-axis limitations in data coordinates are returned from the parameter bottom and top.

Example:

With the help of the appropriate necessary functionality of the python library, we will change the label and limit of the graph in a single program. First, create the data with the numerical values in the variable Numerical_data. Then, establish the seaborn distplot and modify the limit for x-the axis by using the set_xlim function. After this, set the label for the xlabel and ylabel axes along with the title.

import matplotlib.pyplot as plt

import seaborn as sns

 

DataSet = [2, 4, 5, 9, 10, 12, 15, 17, 18, 20, 21 , 23, 24, 28]

fig, ax = plt.subplots()

sns.distplot(DataSet, ax = ax)

ax.set_xlim(1, 50)

plt.show()

The following seaborn plot shows the x-axis and y-axis label with the title:

Conclusion

This article is about the seaborn axis label. We can set the labels for the axes using different techniques. We have given the details about the three methods along with their syntax details that are easy and helps you to modify the axes label. All methods are demonstrated with the example code in the seaborn in Ubuntu 20.04.

About the author

Kalsoom Bibi

Hello, I am a freelance writer and usually write for Linux and other technology related content