import matplotlib.pyplot as plt
fig, ax = plt.subplots()
plt.show()
import pandas as pd
df = pd.read_csv('datasets/football_stats.csv', index_col='player_name')
# Filtrando datos del dataset entre 2014 y 2019
messi_goals = df.loc['Lionel Messi'].query('year != 2020')
ronaldo_goals = df.loc['Cristiano Ronaldo'].query('year != 2020')
# Plot -> ax.plot(X, Y)
fig, ax = plt.subplots()
ax.plot(messi_goals.year, messi_goals.goals)
ax.plot(ronaldo_goals.year, ronaldo_goals.goals)
# plt.savefig('1.jpg')
plt.show()
fig, ax = plt.subplots()
ax.plot(messi_goals.year, messi_goals.goals,
marker='o', color='b', linestyle='--', label='Lionel Messi')
ax.plot(ronaldo_goals.year, ronaldo_goals.goals,
marker='s', color='r', linestyle='dotted', label='Cristiano Ronaldo')
ax.set(xlabel='Years', ylabel='Goals', title='Messi vs. Cristiano goals (2014 - 2019)')
ax.legend()
# plt.savefig('2.jpg')
plt.show()
# Nuevo filtro
cavani_goals = df.loc['Edinson Cavani'].query('year != 2020')
fig, ax = plt.subplots()
ax.plot(messi_goals.year, messi_goals.goals,
marker='o', color='b', linestyle='--', label='Lionel Messi')
ax.plot(messi_goals.year, messi_goals.npg,
marker='.', color='b', linestyle='dotted')
ax.plot(ronaldo_goals.year, ronaldo_goals.goals,
marker='s', color='r', linestyle='--', label='Cristiano Ronaldo')
ax.plot(ronaldo_goals.year, ronaldo_goals.npg,
marker='.', color='r', linestyle='dotted')
ax.plot(cavani_goals.year, cavani_goals.goals,
marker='D', color='m', linestyle='--', label='Edinson Cavani')
ax.plot(cavani_goals.year, cavani_goals.npg,
marker='.', color='m', linestyle='dotted')
ax.set(xlabel='Years', ylabel='Goals', title='Goals and non penalty goals')
ax.legend()
# plt.savefig('3.jpg')
plt.show()
fig, ax = plt.subplots(3, 2)
plt.savefig('4.jpg')
plt.show()
ax.shape
fig, ax = plt.subplots(3, 1, sharey=True, figsize=[6, 10])
ax[0].plot(messi_goals.year, messi_goals.goals,
marker='o', color='b', linestyle='--', label='Total goals')
ax[0].plot(messi_goals.year, messi_goals.npg, marker='.',
color='b', linestyle='dotted', label='Non penalty goals')
ax[1].plot(ronaldo_goals.year, ronaldo_goals.goals,
marker='s', color='r', linestyle='--', label='Total goals')
ax[1].plot(ronaldo_goals.year, ronaldo_goals.npg, marker='.',
color='r', linestyle='dotted', label='Non penalty goals')
ax[2].plot(cavani_goals.year, cavani_goals.goals,
marker='D', color='m', linestyle='--', label='Total goals')
ax[2].plot(cavani_goals.year, cavani_goals.npg, marker='.',
color='m', linestyle='dotted', label='Non penalty goals')
ax[2].set_xlabel('Years')
ax[0].set_ylabel('Lionel Messi')
ax[1].set_ylabel('Cristiano Ronaldo')
ax[2].set_ylabel('Edinson Cavani')
ax[0].set_title('Goals and non penalty goals')
ax[0].legend()
ax[1].legend()
ax[2].legend()
# plt.savefig('5.jpg')
plt.show()
# Datos de tarjetas rojas por equipo
top_red_cards = df.groupby('team_name')['red_cards'].sum() \
.sort_values(ascending=False).head(10)
# Plot
# plt.figure(figsize=[6.4, 6])
plt.style.use('ggplot')
plt.bar(top_red_cards.index, top_red_cards)
plt.xticks(rotation=90)
plt.title('Tarjetas rojas acumuladas')
plt.ylabel('Red cards')
plt.show()
plt.barh(top_red_cards.index, top_red_cards)
plt.title('Tarjetas rojas acumuladas')
plt.xlabel('Red cards')
plt.show()
# Filter
df['goals_assists'] = df.goals + df.assists
top_players = df.groupby('player_name')[['goals', 'assists', 'goals_assists']].sum() \
.sort_values('goals_assists', ascending=False).head(10)
# Plot
plt.bar(top_players.index, top_players.goals, label='Goals')
plt.bar(top_players.index, top_players.assists, label='Assists', bottom=top_players.goals)
plt.xticks(rotation=90)
plt.title('Goles y asistencias de los top players')
plt.ylabel('Goals and assists')
plt.legend()
plt.show()
# Goles totales por equipo en 2018
df_2018 = df.query('year == 2018')
team_goals_2018 = df_2018.groupby('team_name')['goals'].sum()
# Plot
plt.figure(figsize=[7, 6]) # 7 in de ancho y 6 in de alto
plt.hist(team_goals_2018, bins=25)
plt.title('Goles totales por equipo en 2018')
plt.xlabel('Cantidad de goles')
plt.ylabel('Número de equipos con X cantidad de goles')
plt.show()
# Goles totales por equipo en 2018
df_2018 = df.query('year == 2018')
team_goals_2018 = df_2018.groupby('team_name')['goals'].sum()
plt.hist(team_goals_2018, bins=15, histtype='step', density=True)
plt.show()
plt.scatter(df_2018.shots, df_2018.goals, c=df_2018.games)
plt.title('Disparos vs. goles')
plt.xlabel('Disparos')
plt.ylabel('Goles')
plt.show()