2

Good Day,

See the attached image for reference. The x-axis on the Seaborn bar chart I created has overlapping text and is too crowded. How do I fix this?

The data source is on Kaggle and I was following along with this article: https://towardsdatascience.com/a-quick-guide-on-descriptive-statistics-using-pandas-and-seaborn-2aadc7395f32

Here is the code I used:

 sns.set(style = 'darkgrid')
 plt.figure(figsize = (20, 10))
 ax = sns.countplot(x = 'Regionname', data = df)

Seaborn X-axis too crowded

I'd appreciate any help.

Thanks!

deity
  • 23
  • 4

1 Answers1

1

You are not using the figure size you set on the previous line. Try

fig, ax = plt.subplots(figsize=(20, 10))  # generate a figure and return figure and axis handle

sns.countplot(x="Regionname", data=df, ax=ax)  # passing the `ax` to seaborn so it knows about it

An extra thing after this might be to rotate the labels:

ax.set_xticklabels(ax.get_xticklabels(), rotation=60)
Mustafa Aydın
  • 17,645
  • 4
  • 15
  • 38
  • Note that `plt.figure` has no effect because it is called in a different Juypyter cell. Otherwise, it would work fine. – JohanC Mar 28 '21 at 21:28
  • You can also use `axs.set_xticklabels([t.get_text().replace(' ', '\n') for t in axs.get_xticklabels()])` to insert newlines into the labels. – JohanC Mar 28 '21 at 21:36
  • Thanks JohanC. Putting all the code on a single line fixed the problem. A simple solution! – deity Mar 29 '21 at 12:00