How can the axis labels be rotated in Seaborn plots? 2

How can the axis labels be rotated in Seaborn plots?

Axis labels can be rotated in Seaborn plots by using the “rotate” parameter in the “set_xticklabels” and “set_yticklabels” methods. This allows for the customization of the angle at which the labels are displayed, providing a more visually appealing and readable plot. By specifying the desired rotation angle, the axis labels can be adjusted to suit the needs of the user, making it easier to interpret the data being presented. This feature in Seaborn helps to enhance the overall aesthetic and clarity of the plot, making it a useful tool for data visualization.

Rotate Axis Labels in Seaborn Plots


You can use the following basic syntax to rotate the axis labels in a plot in :

my_plot.set_xticklabels(my_plot.get_xticklabels(), rotation=45)

The following example shows how to use this syntax in practice.

Example: How to Rotate Axis Labels in Seaborn Plot

Suppose we have the following pandas DataFrame that contains information about the points scored by basketball players on various teams:

import pandas as pd

#create DataFrame
df = pd.DataFrame({'team': ['Mavericks', 'Mavericks', 'Mavericks',
                            'Mavericks', 'Warriors', 'Warriors',
                            'Blazers', 'Blazers', 'Kings',
                            'some_really_really_long_name'],
                   'points': [22, 14, 9, 7, 29, 20, 30, 34, 19, 12]})

#view DataFrame
print(df)

                           team  points
0                     Mavericks      22
1                     Mavericks      14
2                     Mavericks       9
3                     Mavericks       7
4                      Warriors      29
5                      Warriors      20
6                       Blazers      30
7                       Blazers      34
8                         Kings      19
9  some_really_really_long_name      12

We can use the countplot() function in seaborn to create a plot that displays the count of each team in the DataFrame:

import seaborn as sns

#create seaborn countplot
my_plot = sns.countplot(data=df, x='team')

Since one of the team names is extremely long, it overlaps another team name on the x-axis.

To get around this, we can use the following code to rotate the x-axis labels:

import seaborn as sns

#create seaborn countplot
my_plot = sns.countplot(data=df, x='team')

#rotate x-axis labels
my_plot.set_xticklabels(my_plot.get_xticklabels(), rotation=45)

Notice that each of the x-axis labels are now rotated 45 degrees.

If we’d like, we can also use the horizontalalignment argument to shift the x-axis labels to the left:

import seaborn as sns

#create seaborn countplot
my_plot = sns.countplot(data=df, x='team')

#rotate x-axis labels
my_plot.set_xticklabels(my_plot.get_xticklabels(), rotation=45,
                        horizontalalignment='right')

seaborn rotate axis labels

Note: If you have trouble importing seaborn in a Jupyter notebook, you may first need to run the command %pip install seaborn.

The following tutorials explain how to perform other common tasks in seaborn:

Cite this article

stats writer (2024). How can the axis labels be rotated in Seaborn plots?. PSYCHOLOGICAL SCALES. Retrieved from https://scales.arabpsychology.com/stats/how-can-the-axis-labels-be-rotated-in-seaborn-plots/

stats writer. "How can the axis labels be rotated in Seaborn plots?." PSYCHOLOGICAL SCALES, 25 Jun. 2024, https://scales.arabpsychology.com/stats/how-can-the-axis-labels-be-rotated-in-seaborn-plots/.

stats writer. "How can the axis labels be rotated in Seaborn plots?." PSYCHOLOGICAL SCALES, 2024. https://scales.arabpsychology.com/stats/how-can-the-axis-labels-be-rotated-in-seaborn-plots/.

stats writer (2024) 'How can the axis labels be rotated in Seaborn plots?', PSYCHOLOGICAL SCALES. Available at: https://scales.arabpsychology.com/stats/how-can-the-axis-labels-be-rotated-in-seaborn-plots/.

[1] stats writer, "How can the axis labels be rotated in Seaborn plots?," PSYCHOLOGICAL SCALES, vol. X, no. Y, ص Z-Z, June, 2024.

stats writer. How can the axis labels be rotated in Seaborn plots?. PSYCHOLOGICAL SCALES. 2024;vol(issue):pages.

Download Post (.PDF)
Slide Up
x
PDF
Scroll to Top