user517696
user517696

Reputation: 2672

How do i show the proper count value in seaborn?

CH Gayle          17
YK Pathan         16
AB de Villiers    15
DA Warner         14
SK Raina          13
RG Sharma         13
MEK Hussey        12
AM Rahane         12
MS Dhoni          12
G Gambhir         12

I have a series like this. I want to plot the player on the x axis and their respective value on the y axis. I tried this code:

man_of_match=(matches['player_of_match'].value_counts())
sns.countplot(x=(man_of_match),data=matches,color='B')
sns.plt.show()

But with this code, it plots the frequency of the numeric value, i.e on x axis 12 gets plotted and the count on y axis becomes 4. Similarly for 13 on x axis it shows 2 on y axis.

How do i make the x axis show the name of the player and the y axis the corresponding value of the player.?

Upvotes: 2

Views: 2567

Answers (1)

piRSquared
piRSquared

Reputation: 294258

sns.countplot is meant to do the counting for you. You are counting yourself with value_counts then plotting the counts of counts. Pass matches directly to sns.countplot

ax = sns.countplot(matches['player_of_match'], color='B')
plt.sca(ax)
plt.xticks(rotation=90);

enter image description here


If you want to limit it to the top 10 players. Use value_counts as you did. But use matplotlib directly, to plot.

ax = matches['player_of_match'].value_counts().head(10).plot.bar(width=.8, color='R')
ax.set_xlabel('player_of_match')
ax.set_ylabel('count')

enter image description here

You can get it to look a lot like the seaborn plot

kws = dict(width=.8, color=sns.color_palette('pastel'))
ax = matches['player_of_match'].value_counts().head(10).plot.bar(**kws)
ax.set_xlabel('player_of_match')
ax.set_ylabel('count')
ax.grid(False, axis='x')

enter image description here

Upvotes: 4

Related Questions