1

I have been looking around the net for hours now, and have not been able to solve this problem, and hope some of you can help.

plt.bar(att_new['player'], att_new['shots'].groupby(att_new['player']).transform('sum'))
plt.axhline(y=att_shots_leauge_average, color='r')
plt.xticks(rotation=90)
plt.figure(figsize=(10,30))

enter image description here

my dataframe looks like this:

att_new = att[['id','player','date','team_name','fixture_name','position_new', 'goals','shots', 
                       'shots_on_target', 'xg', 'attacking_pen_area_touches', 
                       'aerials_won', 'final_third_entry_passes', 'dribbles_completed']]

I have been going over: https://datavizpyr.com/sort-bars-in-barplot-using-seaborn-in-python/, but for me, it seems like the groupby I am doing, is making quite some problems but I need it to get the sum value.

Hope you can help! Thanks!

------EDITED CODE------

import pandas as pd
import seaborn as sns

# groupby and sort
dfg = att_new.groupby('player', as_index=False).shots.sum().sort_values('shots', ascending=False)

# get the mean value for everything
mean = att_shots_leauge_average

# plot
ax = dfg.plot.bar('player', 'shots', figsize=(9, 7), legend=False)
ax.axhline(y=mean, color='gray', lw=3)
ax.text(1.5, mean + 0.2, f'mean{mean:0.2f}', weight='bold')

enter image description here

0

1 Answer 1

2
  • You must sort the values with .sort_values()
  • plt.bar(att_new['player'], att_new['shots'].groupby(att_new['player']).transform('sum')) is convoluted, do the .groupby separately, and then plot the result, as shown below.
import pandas as pd
import seaborn as sns  # only used for importing the data

# sample data
tips = sns.load_dataset('tips')

# groupby and sort
dfg = tips.groupby('day', as_index=False).total_bill.sum().sort_values('total_bill', ascending=False)

# get the mean value for everything
mean_tips = tips.total_bill.mean()

# plot
ax = dfg.plot.bar('day', 'total_bill', figsize=(9, 7), legend=False)
ax.axhline(y=mean_tips, color='gray', lw=3)
ax.text(1.5, mean_tips + 0.2, f'Mean Tips: ${mean_tips:0.2f}', weight='bold')

enter image description here

Sign up to request clarification or add additional context in comments.

1 Comment

... I think it is getting late for me. There was.. 4. Thank you for your solution. It is just what I was looking for!

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.