0

I have a problem with a graph. this is the code:

import pandas as pd
import matplotlib.pyplot as plt

df = pd.read_csv('https://raw.githubusercontent.com/verticale3185/FINEL-PROJECT/main/cristiano_vs_messi.csv')

df.date = pd.to_datetime(df.date)
df['year'] = pd.DatetimeIndex(df['date']).year
df['year'].fillna(method = 'ffill', inplace = True)
df['year'] = df['year'].astype(int)

messi_df = df.loc[df.player == 'messi'].reset_index()
ronaldo_df = df.loc[df.player == 'ronaldo']

plt.suptitle('Goals Per Year For Fvery Player')
ronaldo_df.groupby(['year'])['player'].count().astype(int).plot(kind='bar',color='brown',label='Ronaldo',figsize=(15,5),width = 0.4)
messi_df.groupby(['year'])['player'].count().astype(int).plot(kind='bar',color='#14D622',label='Messi',figsize=(15,5),width = 0.2)

Now on this graph I want to add a line about the mean of every player. for example, if the mean of messi`s goals is 32 goals for year, i want one line in the same color that show this.

Or Nir
  • 1
  • 1
  • I Think you have a mistake in with the capital letters of Messi and Ronaldo: messi_df = df.loc[df.player == 'messi'].reset_index() ronaldo_df = df.loc[df.player == 'ronaldo'] – Tomer S Aug 28 '21 at 14:07

1 Answers1

0

You can use ax.axhline() to draw a horizontal line.

r = ronaldo_df.groupby(['year'])['player'].count().astype(int).plot(kind='bar',color='brown',label='Ronaldo',figsize=(15,5),width = 0.4)
m = messi_df.groupby(['year'])['player'].count().astype(int).plot(kind='bar',color='#14D622',label='Messi',figsize=(15,5),width = 0.2)

m.axhline(32, color='#14D622', lw=2)
r.axhline(28, color='brown', lw=2)

enter image description here

r-beginners
  • 31,170
  • 3
  • 14
  • 32
  • I set the number of goals appropriately; I made a pandas plot as an object and added horizontal lines to it. If my answer is helpful to you, please click the check mark to accept the answer. – r-beginners Aug 28 '21 at 14:00
  • Thanks, its nice. maybe do you knoe how to do it when the code alculates the average? – Or Nir Aug 28 '21 at 14:10
  • Which column indicates the goal? Which column indicates the goal? For example, let's say `df['goal'].mean()` for the goal column. – r-beginners Aug 28 '21 at 14:18
  • Your question has become a duplicate question, but I can accept your answer. Nice to meet you. – r-beginners Aug 28 '21 at 14:49