How to prevent overlapping x-axis labels in sns.countplot


Python Problem Overview

For the plot


I got the following graph with x-axis label mixing together, how do I avoid this? Should I change the size of the graph to solve this problem?

enter image description here

Python Solutions

Solution 1 - Python

Having a Series ds like this

import pandas as pd
import seaborn as sns
import matplotlib.pyplot as plt
import numpy as np; np.random.seed(136)

l = "1234567890123"
categories = [ l[i:i+5]+" - "+l[i+1:i+6] for i in range(6)]
x = np.random.choice(categories, size=1000, 
ds = pd.Series({"Column" : x})

there are several options to make the axis labels more readable.

###Change figure size

plt.figure(figsize=(8,4)) # this creates a figure 8 inch wide, 4 inch high
sns.countplot(x="Column", data=ds)

###Rotate the ticklabels

ax = sns.countplot(x="Column", data=ds)

ax.set_xticklabels(ax.get_xticklabels(), rotation=40, ha="right")

enter image description here

###Decrease Fontsize

ax = sns.countplot(x="Column", data=ds)

ax.set_xticklabels(ax.get_xticklabels(), fontsize=7)

enter image description here

Of course any combination of those would work equally well.

###Setting rcParams

The figure size and the xlabel fontsize can be set globally using rcParams

plt.rcParams["figure.figsize"] = (8, 4)
plt.rcParams["xtick.labelsize"] = 7

This might be useful to put on top of a juypter notebook such that those settings apply for any figure generated within. Unfortunately rotating the xticklabels is not possible using rcParams.

I guess it's worth noting that the same strategies would naturally also apply for seaborn barplot, matplotlib bar plot or

Solution 2 - Python

You can rotate the x_labels and increase their font size using the xticks methods of pandas.pyplot.

For Example:

import matplotlib.pyplot as plt
chart = sns.countplot(x="HostRamSize",data=df)


For more such modifications you can refer this link: Drawing from Data

Solution 3 - Python

If you just want to make sure xticks labels are not squeezed together, you can set a proper fig size and try fig.autofmt_xdate().

This function will automatically align and rotate the labels.

Solution 4 - Python

I don't know whether it is an option for you but maybe turning the graphic could be a solution (instead of plotting on x=, do it on y=), such that:


enter image description here

Solution 5 - Python

plt.figure(figsize=(15,10)) #adjust the size of plot

ax.set_xticklabels(ax.get_xticklabels(), rotation=40, ha="right")  #it will rotate text on x axis


you can try this code & change size & rotation according to your need.


All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
Questionwilliam007View Question on Stackoverflow
Solution 1 - PythonImportanceOfBeingErnestView Answer on Stackoverflow
Solution 2 - PythonDeepam GuptaView Answer on Stackoverflow
Solution 3 - Pythonhui chenView Answer on Stackoverflow
Solution 4 - PythonCarles Sans FuentesView Answer on Stackoverflow
Solution 5 - PythonAkash DesaiView Answer on Stackoverflow