How to Set Axis Ticks in Seaborn Plots

Manav Narula Feb 02, 2024
  1. Use the matplotlib.pyplot.set_xtickslabels() and matplotlib.pyplot.set_ytickslabels() Functions to Set the Axis Tick Labels on Seaborn Plots in Python
  2. Use the matplotlib.pyplot.xticks() and matplotlib.pyplot.yticks() Functions to Set the Axis Tick Labels on Seaborn Plots in Python
How to Set Axis Ticks in Seaborn Plots

This tutorial will introduce different functions to set the axis ticks for seaborn plots in Python.

Note that in this article, we discuss the examples related to x-axis tick labels. We can use the methods for the y-axis in the exact same way.

Use the matplotlib.pyplot.set_xtickslabels() and matplotlib.pyplot.set_ytickslabels() Functions to Set the Axis Tick Labels on Seaborn Plots in Python

These functions are used to provide custom labels for the plot. They are taken from the matplotlib library and can be used for seaborn plots. They are generally used after the set_xticks and set_yticks functions are used to specify the position of the tick labels.

It also allows us to alter the font and the size of the tick labels and even rotate them if required using different parameters.

For example,

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

s_x = [1, 2, 3, 4, 5]
s_y = random.sample(range(0, 100), 5)

df = pd.DataFrame({"s_x": s_x, "s_y": s_y})

g = sns.scatterplot(data=df, y=s_y, x=s_x)
g.set_xticks(range(len(s_x) + 1))
g.set_xticklabels(["0", "a", "b", "c", "d", "e"])

set seaborn axis ticks

Similarly, the set_yticklabels() can be used to customize the y-axis tick labels.

Note that this function is used on the axes object of the plot.

Use the matplotlib.pyplot.xticks() and matplotlib.pyplot.yticks() Functions to Set the Axis Tick Labels on Seaborn Plots in Python

These functions can be used for many purposes. If we use them without parameters, they will return the location and label values of the default tick labels on the axis. However, we can use them to set custom tick values if we specify them with the location and label values.

See the following code for the use of the xticks() function.

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

s_x = [1, 2, 3, 4, 5]
s_y = random.sample(range(0, 100), 5)

df = pd.DataFrame({"s_x": s_x, "s_y": s_y})

g = sns.scatterplot(data=df, y=s_y, x=s_x)
plt.xticks([1, 2, 3, 4, 5], ["a", "b", "c", "d", "e"])

set seaborn axis ticks

Similarly, the yticks() can be used to customize the y-axis tick labels. Here also, we can use different parameters to control the size and rotation of the tick values.

Additionally, one can use the get_xticklabels() or get_yticklabels() to get the default tick values.

Author: Manav Narula
Manav Narula avatar Manav Narula avatar

Manav is a IT Professional who has a lot of experience as a core developer in many live projects. He is an avid learner who enjoys learning new things and sharing his findings whenever possible.

LinkedIn

Related Article - Seaborn Tick

Related Article - Seaborn Axis