0

I used the following code to generate the countplot in python using seaborn:

sns.countplot( x='Genres', data=gn_s)

But I got the following output:

output

I can't see the items on x-axis clearly as they are overlapping. How can I correct that? Also I would like all the items to be arranged in a decreasing order of count. How can I achieve that?

user2436437
  • 357
  • 1
  • 6
  • 15

2 Answers2

1

You can use choose the x-axis to be vertical, as an example:

g = sns.countplot( x='Genres', data=gn_s)
g.set_xticklabels(g.get_xticklabels(),rotation=90)

Or, you can also do:

plt.xticks(rotation=90)
ndrwnaguib
  • 5,623
  • 3
  • 28
  • 51
  • I am getting the following error: set_xticklabels() missing 1 required positional argument: 'labels' – user2436437 Dec 31 '17 at 14:53
  • Thanks I got the labels flipped. Any hint on how to arrange it a increasing/decreasing order? – user2436437 Dec 31 '17 at 15:12
  • 1
    You can use the `order` parameter in `sns.countplot`, as an example, `sns.countplot( x='Genres', data=gn_s, order = gn_s['Genres'].value_counts().index)` – ndrwnaguib Dec 31 '17 at 15:14
  • You can read about [seaborn.countplot parameters](https://seaborn.pydata.org/generated/seaborn.countplot.html). – ndrwnaguib Dec 31 '17 at 15:19
1

Bring in matplotlib to set up an axis ahead of time, so that you can modify the axis tick labels by rotating them 90 degrees and/or changing font size. To arrange your samples in order, you need to modify the source. I assume you're starting with a pandas dataframe, so something like:

data = data.sort_values(by='Genres', ascending=False) labels = # list of labels in the correct order, probably your data.index fig, ax1 = plt.subplots(1,1) sns.countplot( x='Genres', data=gn_s, ax=ax1) ax1.set_xticklabels(labels, rotation=90)

would probably help.

edit Taking andrewnagyeb's suggestion from the comments to order the plot:

sns.countplot( x='Genres', data=gn_s, order = gn_s['Genres'].value_counts().index)

iayork
  • 6,420
  • 8
  • 44
  • 49
  • Thanks that worked partially, the labels are flipped vertically. But I still don't have them in decreasing order since it is a count plot. It is counting how many times a particular word is appearing and finally plotting the results. – user2436437 Dec 31 '17 at 15:10