#http://www.seanlahman.com/baseball-archive/statistics/

from pybaseball import lahman as la
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import seaborn as sns

pd.set_option('display.max_columns', None)
#remove chained assignment
#you can read more about chained assignment here: https://pandas.pydata.org/pandas-docs/version/0.22.0/indexing.html
pd.set_option('chained_assignment', None)

#setting these for later use
OAK_PRIMARY_COLOR = '#003831'
OAK_SECONDARY_COLOR = '#EFB21E'
STL_PRIMARY_COLOR = '#FF0000'
STL_SECONDARY_COLOR = '#0000FF'

# each of these functions gets us back a DataFrame.
salaries = la.salaries()
names = la.people()
teams = la.teams()
batting = la.batting()
home_games = la.home_games()

plt.style.use('seaborn-whitegrid')

salaries_teams_years = salaries.groupby(['teamID','yearID'], as_index = False).sum()

oak = salaries_teams_years.loc[salaries_teams_years['teamID'] == 'OAK']
stl = salaries_teams_years.loc[salaries_teams_years['teamID'] == 'SLN']

fig, ax = plt.subplots(figsize = (10, 7))

ax.plot(stl['yearID'], stl['salary']/1000000, label = 'STL', color = STL_SECONDARY_COLOR, linewidth = 1.5)
ax.plot(oak['yearID'], oak['salary']/1000000, label = 'OAK', color = OAK_SECONDARY_COLOR, linewidth = 1.5)

# getting the line data for the Cardinals, A's, and line plot
# ax.get_lines docs -> https://matplotlib.org/3.1.1/api/_as_gen/matplotlib.axes.Axes.get_lines.html
# this function returns a list of lines on the current axis.
# we can then call get xy_data() to get the x and y data
# line.get_xydata docs -> https://www.kite.com/python/docs/matplotlib.lines.Line2D.get_xydata
# it's in the form of a 2-dimensional Numpy Array, where the first column is the x data and the second column
# is the y data
line_one = ax.get_lines()[0].get_xydata()
line_two = ax.get_lines()[1].get_xydata()

x_one, y_one = line_one[:, 0], line_one[:, 1]
x_two, y_two = line_two[:, 0], line_two[:, 1]

# fill the area underneath the curve for both Cardinals and A's
# ax.fill_between docs -> https://matplotlib.org/3.1.1/api/_as_gen/matplotlib.axes.Axes.fill_between.html
ax.fill_between(x_one, y_one, color = STL_PRIMARY_COLOR, alpha = 0.2)
ax.fill_between(x_two, y_two, color = OAK_PRIMARY_COLOR, alpha = 0.2)

ax.set_xlabel('Year');
ax.set_ylabel('Team Salary in Millions',);
ax.set_title("Money Spent by Cardinals and A's", color = 'black');
ax.set_xlim([1985, 2016])

#prevent scientific notation
#reading: https://stackoverflow.com/questions/28371674/prevent-scientific-notation-in-matplotlib-pyplot
ax.ticklabel_format(useOffset = False, style = 'plain')

ax.legend(loc = 'upper left');
plt.savefig('salaryDataForCardsAndAs.png')
plt.show()


#looking at the cardinals top 10 years in payroll 
print(stl.sort_values(by = 'salary', ascending = False).head(10))

#wins vs money spent: The cardinals obviously spent more than the A's, but did the money spent translate to wins?
salaries_teams_years = salaries.groupby(['teamID', 'yearID'], as_index = False).sum()
print(salaries_teams_years.sort_values(by = 'salary', ascending = False).head(10))

#stl_wins = money_wins[(money_wins['teamID'] == 'SLN') & (money_wins['W'] > 90)]

money_wins = salaries_teams_years.merge(teams, on = ['teamID', 'yearID'])
oak_stl = money_wins[(money_wins['teamID'] == 'OAK') | (money_wins['teamID'] == 'SLN')]

oak_stl['salary/win'] = oak_stl['salary'] / oak_stl['W']

plt.style.use('ggplot')

oak = oak_stl.loc[oak_stl['teamID'] == 'OAK']
stl = oak_stl.loc[oak_stl['teamID'] == 'SLN']

fig, ax = plt.subplots(figsize = (10, 8))

#setting the s keyword arg to make point size a function of team W's -> the more wins you had as a team, the bigger the point is going to be on the graph
ax.scatter(x = oak['yearID'], y = oak['salary/win'], color = OAK_PRIMARY_COLOR, alpha = 0.6, edgecolors = OAK_SECONDARY_COLOR, linewidth = 1.5, s = oak['W'] * 1.5)
ax.scatter(x = stl['yearID'], y = stl['salary/win'], color = STL_PRIMARY_COLOR, alpha = 0.6, edgecolors = STL_SECONDARY_COLOR, linewidth = 1.5, s = stl['W'] * 1.5)

#preventing scientific notation again
ax.ticklabel_format(useOffset = False, style = 'plain')

ax.set_xlim([1985, 2016])
ax.set_ylim(0)
ax.set_xlabel('Year')
ax.set_ylabel('Dollar Spent per Win')
ax.set_title('Oakland and St. Louis\'s dollars spent per win')

plt.legend(['OAK', 'STL'], loc = 'upper left')
fig.text(0.8, -.05, 'Point size is a function of team wins', ha = 'center')

plt.show()
plt.savefig('WinsVsMoneySpent.png')

#plotting fan attendance
plt.style.use('fivethirtyeight')

fig, ax = plt.subplots(figsize = (10, 7))

#prevent scientific notation in the axis (hopefully this works, it didnt in the above graph)
ax.ticklabel_format(useOffset = False, style = 'plain')

ax.plot(oak['yearID'], oak['attendance'], color = OAK_SECONDARY_COLOR)
ax.plot(stl['yearID'], stl['attendance'], color = STL_SECONDARY_COLOR)

ax.legend(['OAK', 'STL'])
ax.set_title('Oakland and St. Louis attendance')

plt.show()
plt.savefig('OaklandVsSTLAttendance.png')

#cardinals spend more on players, but also have much more attendance outtings, does this make up for their higher payroll?
oak['salary/attend'] = oak['salary'] / oak['attendance']
stl['salary/attend'] = stl['salary'] / stl['attendance']

fig, ax = plt.subplots(figsize = (10, 8))

ax.ticklabel_format(useOffset = False, style = 'plain')

ax.plot(oak['yearID'], oak['salary/attend'], color = OAK_SECONDARY_COLOR)
ax.plot(stl['yearID'], stl['salary/attend'], color = STL_SECONDARY_COLOR)

ax.legend(['OAK', 'STL'], loc = 'upper left')
ax.set_title('Dollar spent per fan for A\'s and Cardinals')

plt.savefig('dollarSpentPerFan.png')
plt.show()

plt.style.use('ggplot')
money_wins_11 = money_wins.loc[money_wins['yearID'] == 2011]

#nl and al dataframs
nl = money_wins_11.loc[money_wins_11['lgID'] == 'NL']
al = money_wins_11.loc[money_wins_11['lgID'] == 'AL']

fig, ax = plt.subplots()

ax.scatter(nl['salary']/100000, nl['W'], color = 'blue', label = 'NL')
ax.scatter(al['salary']/100000, al['W'], color = 'red', label = 'AL')

ax.set_xlabel('Salary (Millions)')
ax.set_ylabel('Wins')
ax.set_title('Salary vs Wins')

ax.legend()
plt.show()



































9bb71f19ad9f822cd1d3800aea546ef07251b9bf
<mask token>

<mask token>
pd.set_option('display.max_columns', None)
pd.set_option('chained_assignment', None)
<mask token>
plt.style.use('seaborn-whitegrid')
<mask token>
ax.plot(stl['yearID'], stl['salary'] / 1000000, label='STL', color=
    STL_SECONDARY_COLOR, linewidth=1.5)
ax.plot(oak['yearID'], oak['salary'] / 1000000, label='OAK', color=
    OAK_SECONDARY_COLOR, linewidth=1.5)
<mask token>
ax.fill_between(x_one, y_one, color=STL_PRIMARY_COLOR, alpha=0.2)
ax.fill_between(x_two, y_two, color=OAK_PRIMARY_COLOR, alpha=0.2)
ax.set_xlabel('Year')
ax.set_ylabel('Team Salary in Millions')
ax.set_title("Money Spent by Cardinals and A's", color='black')
ax.set_xlim([1985, 2016])
ax.ticklabel_format(useOffset=False, style='plain')
ax.legend(loc='upper left')
plt.savefig('salaryDataForCardsAndAs.png')
plt.show()
print(stl.sort_values(by='salary', ascending=False).head(10))
<mask token>
print(salaries_teams_years.sort_values(by='salary', ascending=False).head(10))
<mask token>
plt.style.use('ggplot')
<mask token>
ax.scatter(x=oak['yearID'], y=oak['salary/win'], color=OAK_PRIMARY_COLOR,
    alpha=0.6, edgecolors=OAK_SECONDARY_COLOR, linewidth=1.5, s=oak['W'] * 1.5)
ax.scatter(x=stl['yearID'], y=stl['salary/win'], color=STL_PRIMARY_COLOR,
    alpha=0.6, edgecolors=STL_SECONDARY_COLOR, linewidth=1.5, s=stl['W'] * 1.5)
ax.ticklabel_format(useOffset=False, style='plain')
ax.set_xlim([1985, 2016])
ax.set_ylim(0)
ax.set_xlabel('Year')
ax.set_ylabel('Dollar Spent per Win')
ax.set_title("Oakland and St. Louis's dollars spent per win")
plt.legend(['OAK', 'STL'], loc='upper left')
fig.text(0.8, -0.05, 'Point size is a function of team wins', ha='center')
plt.show()
plt.savefig('WinsVsMoneySpent.png')
plt.style.use('fivethirtyeight')
<mask token>
ax.ticklabel_format(useOffset=False, style='plain')
ax.plot(oak['yearID'], oak['attendance'], color=OAK_SECONDARY_COLOR)
ax.plot(stl['yearID'], stl['attendance'], color=STL_SECONDARY_COLOR)
ax.legend(['OAK', 'STL'])
ax.set_title('Oakland and St. Louis attendance')
plt.show()
plt.savefig('OaklandVsSTLAttendance.png')
<mask token>
ax.ticklabel_format(useOffset=False, style='plain')
ax.plot(oak['yearID'], oak['salary/attend'], color=OAK_SECONDARY_COLOR)
ax.plot(stl['yearID'], stl['salary/attend'], color=STL_SECONDARY_COLOR)
ax.legend(['OAK', 'STL'], loc='upper left')
ax.set_title("Dollar spent per fan for A's and Cardinals")
plt.savefig('dollarSpentPerFan.png')
plt.show()
plt.style.use('ggplot')
<mask token>
ax.scatter(nl['salary'] / 100000, nl['W'], color='blue', label='NL')
ax.scatter(al['salary'] / 100000, al['W'], color='red', label='AL')
ax.set_xlabel('Salary (Millions)')
ax.set_ylabel('Wins')
ax.set_title('Salary vs Wins')
ax.legend()
plt.show()

<mask token>
pd.set_option('display.max_columns', None)
pd.set_option('chained_assignment', None)
OAK_PRIMARY_COLOR = '#003831'
OAK_SECONDARY_COLOR = '#EFB21E'
STL_PRIMARY_COLOR = '#FF0000'
STL_SECONDARY_COLOR = '#0000FF'
salaries = la.salaries()
names = la.people()
teams = la.teams()
batting = la.batting()
home_games = la.home_games()
plt.style.use('seaborn-whitegrid')
salaries_teams_years = salaries.groupby(['teamID', 'yearID'], as_index=False
    ).sum()
oak = salaries_teams_years.loc[salaries_teams_years['teamID'] == 'OAK']
stl = salaries_teams_years.loc[salaries_teams_years['teamID'] == 'SLN']
fig, ax = plt.subplots(figsize=(10, 7))
ax.plot(stl['yearID'], stl['salary'] / 1000000, label='STL', color=
    STL_SECONDARY_COLOR, linewidth=1.5)
ax.plot(oak['yearID'], oak['salary'] / 1000000, label='OAK', color=
    OAK_SECONDARY_COLOR, linewidth=1.5)
line_one = ax.get_lines()[0].get_xydata()
line_two = ax.get_lines()[1].get_xydata()
x_one, y_one = line_one[:, 0], line_one[:, 1]
x_two, y_two = line_two[:, 0], line_two[:, 1]
ax.fill_between(x_one, y_one, color=STL_PRIMARY_COLOR, alpha=0.2)
ax.fill_between(x_two, y_two, color=OAK_PRIMARY_COLOR, alpha=0.2)
ax.set_xlabel('Year')
ax.set_ylabel('Team Salary in Millions')
ax.set_title("Money Spent by Cardinals and A's", color='black')
ax.set_xlim([1985, 2016])
ax.ticklabel_format(useOffset=False, style='plain')
ax.legend(loc='upper left')
plt.savefig('salaryDataForCardsAndAs.png')
plt.show()
print(stl.sort_values(by='salary', ascending=False).head(10))
salaries_teams_years = salaries.groupby(['teamID', 'yearID'], as_index=False
    ).sum()
print(salaries_teams_years.sort_values(by='salary', ascending=False).head(10))
money_wins = salaries_teams_years.merge(teams, on=['teamID', 'yearID'])
oak_stl = money_wins[(money_wins['teamID'] == 'OAK') | (money_wins['teamID'
    ] == 'SLN')]
oak_stl['salary/win'] = oak_stl['salary'] / oak_stl['W']
plt.style.use('ggplot')
oak = oak_stl.loc[oak_stl['teamID'] == 'OAK']
stl = oak_stl.loc[oak_stl['teamID'] == 'SLN']
fig, ax = plt.subplots(figsize=(10, 8))
ax.scatter(x=oak['yearID'], y=oak['salary/win'], color=OAK_PRIMARY_COLOR,
    alpha=0.6, edgecolors=OAK_SECONDARY_COLOR, linewidth=1.5, s=oak['W'] * 1.5)
ax.scatter(x=stl['yearID'], y=stl['salary/win'], color=STL_PRIMARY_COLOR,
    alpha=0.6, edgecolors=STL_SECONDARY_COLOR, linewidth=1.5, s=stl['W'] * 1.5)
ax.ticklabel_format(useOffset=False, style='plain')
ax.set_xlim([1985, 2016])
ax.set_ylim(0)
ax.set_xlabel('Year')
ax.set_ylabel('Dollar Spent per Win')
ax.set_title("Oakland and St. Louis's dollars spent per win")
plt.legend(['OAK', 'STL'], loc='upper left')
fig.text(0.8, -0.05, 'Point size is a function of team wins', ha='center')
plt.show()
plt.savefig('WinsVsMoneySpent.png')
plt.style.use('fivethirtyeight')
fig, ax = plt.subplots(figsize=(10, 7))
ax.ticklabel_format(useOffset=False, style='plain')
ax.plot(oak['yearID'], oak['attendance'], color=OAK_SECONDARY_COLOR)
ax.plot(stl['yearID'], stl['attendance'], color=STL_SECONDARY_COLOR)
ax.legend(['OAK', 'STL'])
ax.set_title('Oakland and St. Louis attendance')
plt.show()
plt.savefig('OaklandVsSTLAttendance.png')
oak['salary/attend'] = oak['salary'] / oak['attendance']
stl['salary/attend'] = stl['salary'] / stl['attendance']
fig, ax = plt.subplots(figsize=(10, 8))
ax.ticklabel_format(useOffset=False, style='plain')
ax.plot(oak['yearID'], oak['salary/attend'], color=OAK_SECONDARY_COLOR)
ax.plot(stl['yearID'], stl['salary/attend'], color=STL_SECONDARY_COLOR)
ax.legend(['OAK', 'STL'], loc='upper left')
ax.set_title("Dollar spent per fan for A's and Cardinals")
plt.savefig('dollarSpentPerFan.png')
plt.show()
plt.style.use('ggplot')
money_wins_11 = money_wins.loc[money_wins['yearID'] == 2011]
nl = money_wins_11.loc[money_wins_11['lgID'] == 'NL']
al = money_wins_11.loc[money_wins_11['lgID'] == 'AL']
fig, ax = plt.subplots()
ax.scatter(nl['salary'] / 100000, nl['W'], color='blue', label='NL')
ax.scatter(al['salary'] / 100000, al['W'], color='red', label='AL')
ax.set_xlabel('Salary (Millions)')
ax.set_ylabel('Wins')
ax.set_title('Salary vs Wins')
ax.legend()
plt.show()

from pybaseball import lahman as la
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import seaborn as sns
pd.set_option('display.max_columns', None)
pd.set_option('chained_assignment', None)
OAK_PRIMARY_COLOR = '#003831'
OAK_SECONDARY_COLOR = '#EFB21E'
STL_PRIMARY_COLOR = '#FF0000'
STL_SECONDARY_COLOR = '#0000FF'
salaries = la.salaries()
names = la.people()
teams = la.teams()
batting = la.batting()
home_games = la.home_games()
plt.style.use('seaborn-whitegrid')
salaries_teams_years = salaries.groupby(['teamID', 'yearID'], as_index=False
    ).sum()
oak = salaries_teams_years.loc[salaries_teams_years['teamID'] == 'OAK']
stl = salaries_teams_years.loc[salaries_teams_years['teamID'] == 'SLN']
fig, ax = plt.subplots(figsize=(10, 7))
ax.plot(stl['yearID'], stl['salary'] / 1000000, label='STL', color=
    STL_SECONDARY_COLOR, linewidth=1.5)
ax.plot(oak['yearID'], oak['salary'] / 1000000, label='OAK', color=
    OAK_SECONDARY_COLOR, linewidth=1.5)
line_one = ax.get_lines()[0].get_xydata()
line_two = ax.get_lines()[1].get_xydata()
x_one, y_one = line_one[:, 0], line_one[:, 1]
x_two, y_two = line_two[:, 0], line_two[:, 1]
ax.fill_between(x_one, y_one, color=STL_PRIMARY_COLOR, alpha=0.2)
ax.fill_between(x_two, y_two, color=OAK_PRIMARY_COLOR, alpha=0.2)
ax.set_xlabel('Year')
ax.set_ylabel('Team Salary in Millions')
ax.set_title("Money Spent by Cardinals and A's", color='black')
ax.set_xlim([1985, 2016])
ax.ticklabel_format(useOffset=False, style='plain')
ax.legend(loc='upper left')
plt.savefig('salaryDataForCardsAndAs.png')
plt.show()
print(stl.sort_values(by='salary', ascending=False).head(10))
salaries_teams_years = salaries.groupby(['teamID', 'yearID'], as_index=False
    ).sum()
print(salaries_teams_years.sort_values(by='salary', ascending=False).head(10))
money_wins = salaries_teams_years.merge(teams, on=['teamID', 'yearID'])
oak_stl = money_wins[(money_wins['teamID'] == 'OAK') | (money_wins['teamID'
    ] == 'SLN')]
oak_stl['salary/win'] = oak_stl['salary'] / oak_stl['W']
plt.style.use('ggplot')
oak = oak_stl.loc[oak_stl['teamID'] == 'OAK']
stl = oak_stl.loc[oak_stl['teamID'] == 'SLN']
fig, ax = plt.subplots(figsize=(10, 8))
ax.scatter(x=oak['yearID'], y=oak['salary/win'], color=OAK_PRIMARY_COLOR,
    alpha=0.6, edgecolors=OAK_SECONDARY_COLOR, linewidth=1.5, s=oak['W'] * 1.5)
ax.scatter(x=stl['yearID'], y=stl['salary/win'], color=STL_PRIMARY_COLOR,
    alpha=0.6, edgecolors=STL_SECONDARY_COLOR, linewidth=1.5, s=stl['W'] * 1.5)
ax.ticklabel_format(useOffset=False, style='plain')
ax.set_xlim([1985, 2016])
ax.set_ylim(0)
ax.set_xlabel('Year')
ax.set_ylabel('Dollar Spent per Win')
ax.set_title("Oakland and St. Louis's dollars spent per win")
plt.legend(['OAK', 'STL'], loc='upper left')
fig.text(0.8, -0.05, 'Point size is a function of team wins', ha='center')
plt.show()
plt.savefig('WinsVsMoneySpent.png')
plt.style.use('fivethirtyeight')
fig, ax = plt.subplots(figsize=(10, 7))
ax.ticklabel_format(useOffset=False, style='plain')
ax.plot(oak['yearID'], oak['attendance'], color=OAK_SECONDARY_COLOR)
ax.plot(stl['yearID'], stl['attendance'], color=STL_SECONDARY_COLOR)
ax.legend(['OAK', 'STL'])
ax.set_title('Oakland and St. Louis attendance')
plt.show()
plt.savefig('OaklandVsSTLAttendance.png')
oak['salary/attend'] = oak['salary'] / oak['attendance']
stl['salary/attend'] = stl['salary'] / stl['attendance']
fig, ax = plt.subplots(figsize=(10, 8))
ax.ticklabel_format(useOffset=False, style='plain')
ax.plot(oak['yearID'], oak['salary/attend'], color=OAK_SECONDARY_COLOR)
ax.plot(stl['yearID'], stl['salary/attend'], color=STL_SECONDARY_COLOR)
ax.legend(['OAK', 'STL'], loc='upper left')
ax.set_title("Dollar spent per fan for A's and Cardinals")
plt.savefig('dollarSpentPerFan.png')
plt.show()
plt.style.use('ggplot')
money_wins_11 = money_wins.loc[money_wins['yearID'] == 2011]
nl = money_wins_11.loc[money_wins_11['lgID'] == 'NL']
al = money_wins_11.loc[money_wins_11['lgID'] == 'AL']
fig, ax = plt.subplots()
ax.scatter(nl['salary'] / 100000, nl['W'], color='blue', label='NL')
ax.scatter(al['salary'] / 100000, al['W'], color='red', label='AL')
ax.set_xlabel('Salary (Millions)')
ax.set_ylabel('Wins')
ax.set_title('Salary vs Wins')
ax.legend()
plt.show()

#http://www.seanlahman.com/baseball-archive/statistics/

from pybaseball import lahman as la
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import seaborn as sns

pd.set_option('display.max_columns', None)
#remove chained assignment
#you can read more about chained assignment here: https://pandas.pydata.org/pandas-docs/version/0.22.0/indexing.html
pd.set_option('chained_assignment', None)

#setting these for later use
OAK_PRIMARY_COLOR = '#003831'
OAK_SECONDARY_COLOR = '#EFB21E'
STL_PRIMARY_COLOR = '#FF0000'
STL_SECONDARY_COLOR = '#0000FF'

# each of these functions gets us back a DataFrame.
salaries = la.salaries()
names = la.people()
teams = la.teams()
batting = la.batting()
home_games = la.home_games()

plt.style.use('seaborn-whitegrid')

salaries_teams_years = salaries.groupby(['teamID','yearID'], as_index = False).sum()

oak = salaries_teams_years.loc[salaries_teams_years['teamID'] == 'OAK']
stl = salaries_teams_years.loc[salaries_teams_years['teamID'] == 'SLN']

fig, ax = plt.subplots(figsize = (10, 7))

ax.plot(stl['yearID'], stl['salary']/1000000, label = 'STL', color = STL_SECONDARY_COLOR, linewidth = 1.5)
ax.plot(oak['yearID'], oak['salary']/1000000, label = 'OAK', color = OAK_SECONDARY_COLOR, linewidth = 1.5)

# getting the line data for the Cardinals, A's, and line plot
# ax.get_lines docs -> https://matplotlib.org/3.1.1/api/_as_gen/matplotlib.axes.Axes.get_lines.html
# this function returns a list of lines on the current axis.
# we can then call get xy_data() to get the x and y data
# line.get_xydata docs -> https://www.kite.com/python/docs/matplotlib.lines.Line2D.get_xydata
# it's in the form of a 2-dimensional Numpy Array, where the first column is the x data and the second column
# is the y data
line_one = ax.get_lines()[0].get_xydata()
line_two = ax.get_lines()[1].get_xydata()

x_one, y_one = line_one[:, 0], line_one[:, 1]
x_two, y_two = line_two[:, 0], line_two[:, 1]

# fill the area underneath the curve for both Cardinals and A's
# ax.fill_between docs -> https://matplotlib.org/3.1.1/api/_as_gen/matplotlib.axes.Axes.fill_between.html
ax.fill_between(x_one, y_one, color = STL_PRIMARY_COLOR, alpha = 0.2)
ax.fill_between(x_two, y_two, color = OAK_PRIMARY_COLOR, alpha = 0.2)

ax.set_xlabel('Year');
ax.set_ylabel('Team Salary in Millions',);
ax.set_title("Money Spent by Cardinals and A's", color = 'black');
ax.set_xlim([1985, 2016])

#prevent scientific notation
#reading: https://stackoverflow.com/questions/28371674/prevent-scientific-notation-in-matplotlib-pyplot
ax.ticklabel_format(useOffset = False, style = 'plain')

ax.legend(loc = 'upper left');
plt.savefig('salaryDataForCardsAndAs.png')
plt.show()


#looking at the cardinals top 10 years in payroll 
print(stl.sort_values(by = 'salary', ascending = False).head(10))

#wins vs money spent: The cardinals obviously spent more than the A's, but did the money spent translate to wins?
salaries_teams_years = salaries.groupby(['teamID', 'yearID'], as_index = False).sum()
print(salaries_teams_years.sort_values(by = 'salary', ascending = False).head(10))

#stl_wins = money_wins[(money_wins['teamID'] == 'SLN') & (money_wins['W'] > 90)]

money_wins = salaries_teams_years.merge(teams, on = ['teamID', 'yearID'])
oak_stl = money_wins[(money_wins['teamID'] == 'OAK') | (money_wins['teamID'] == 'SLN')]

oak_stl['salary/win'] = oak_stl['salary'] / oak_stl['W']

plt.style.use('ggplot')

oak = oak_stl.loc[oak_stl['teamID'] == 'OAK']
stl = oak_stl.loc[oak_stl['teamID'] == 'SLN']

fig, ax = plt.subplots(figsize = (10, 8))

#setting the s keyword arg to make point size a function of team W's -> the more wins you had as a team, the bigger the point is going to be on the graph
ax.scatter(x = oak['yearID'], y = oak['salary/win'], color = OAK_PRIMARY_COLOR, alpha = 0.6, edgecolors = OAK_SECONDARY_COLOR, linewidth = 1.5, s = oak['W'] * 1.5)
ax.scatter(x = stl['yearID'], y = stl['salary/win'], color = STL_PRIMARY_COLOR, alpha = 0.6, edgecolors = STL_SECONDARY_COLOR, linewidth = 1.5, s = stl['W'] * 1.5)

#preventing scientific notation again
ax.ticklabel_format(useOffset = False, style = 'plain')

ax.set_xlim([1985, 2016])
ax.set_ylim(0)
ax.set_xlabel('Year')
ax.set_ylabel('Dollar Spent per Win')
ax.set_title('Oakland and St. Louis\'s dollars spent per win')

plt.legend(['OAK', 'STL'], loc = 'upper left')
fig.text(0.8, -.05, 'Point size is a function of team wins', ha = 'center')

plt.show()
plt.savefig('WinsVsMoneySpent.png')

#plotting fan attendance
plt.style.use('fivethirtyeight')

fig, ax = plt.subplots(figsize = (10, 7))

#prevent scientific notation in the axis (hopefully this works, it didnt in the above graph)
ax.ticklabel_format(useOffset = False, style = 'plain')

ax.plot(oak['yearID'], oak['attendance'], color = OAK_SECONDARY_COLOR)
ax.plot(stl['yearID'], stl['attendance'], color = STL_SECONDARY_COLOR)

ax.legend(['OAK', 'STL'])
ax.set_title('Oakland and St. Louis attendance')

plt.show()
plt.savefig('OaklandVsSTLAttendance.png')

#cardinals spend more on players, but also have much more attendance outtings, does this make up for their higher payroll?
oak['salary/attend'] = oak['salary'] / oak['attendance']
stl['salary/attend'] = stl['salary'] / stl['attendance']

fig, ax = plt.subplots(figsize = (10, 8))

ax.ticklabel_format(useOffset = False, style = 'plain')

ax.plot(oak['yearID'], oak['salary/attend'], color = OAK_SECONDARY_COLOR)
ax.plot(stl['yearID'], stl['salary/attend'], color = STL_SECONDARY_COLOR)

ax.legend(['OAK', 'STL'], loc = 'upper left')
ax.set_title('Dollar spent per fan for A\'s and Cardinals')

plt.savefig('dollarSpentPerFan.png')
plt.show()

plt.style.use('ggplot')
money_wins_11 = money_wins.loc[money_wins['yearID'] == 2011]

#nl and al dataframs
nl = money_wins_11.loc[money_wins_11['lgID'] == 'NL']
al = money_wins_11.loc[money_wins_11['lgID'] == 'AL']

fig, ax = plt.subplots()

ax.scatter(nl['salary']/100000, nl['W'], color = 'blue', label = 'NL')
ax.scatter(al['salary']/100000, al['W'], color = 'red', label = 'AL')

ax.set_xlabel('Salary (Millions)')
ax.set_ylabel('Wins')
ax.set_title('Salary vs Wins')

ax.legend()
plt.show()


































