I have the below code, where I want user input to build a bar chart. However, the plot never actually reveals itself until the user exits the function?
def get_plot():
while True:
again = input("Do you want to visualise a player's pb scores?")
if again == 'yes':
while True:
player_name = input("What player do you want to visualise PB scores for the 19/20 season?").title()
if player_name in historics.values:
print("Player is present within the underlying data")
break
else:
print("Player does not exist within the underlying data")
is_player = historics['Player Name'] == player_name
new_table = historics[is_player]
#now we will use this data to produce a bar chart for this player, showing their pb scores over the 19/20 season
def get_graph():
plt.bar(new_table.Date,new_table['Matchday Score'],color="g")
plt.title("%s's PB scores for 19/20 season" % player_name)
plt.xlabel("Date")
plt.ylabel("Matchday Score")
if 'Forward' in new_table['FI Player Current Position'].values:
plt.axhline(y=255.56, color='gold', linestyle='-')
plt.axhline(y=239.94, color='slategrey', linestyle='-')
plt.axhline(y=173.52, color='saddlebrown', linestyle='-')
elif 'Midfielder' in new_table['FI Player Current Position'].values:
plt.axhline(y=262.44, color='gold', linestyle='-')
plt.axhline(y=263.35, color='slategrey', linestyle='-')
plt.axhline(y=205.66, color='saddlebrown', linestyle='-')
else:
plt.axhline(y=232.62, color='gold', linestyle='-')
plt.axhline(y=227.43, color='slategrey', linestyle='-')
plt.axhline(y=182.36, color='saddlebrown', linestyle='-')
plt.rcParams["figure.figsize"]=10,5
plt.show()
get_graph()
if again == 'no':
break
else:
print("Please provide a valid value")
get_plot()
So after the input of "no" is provided, the plot of the last provided player_name value is displayed. However, I want the plot to be created (and displayed) upon the entry of the player_name value, and for the plot to be refreshed upon each subsequent value provided?
Where am I going wrong?
Thanks.
EDIT: An example of the data (historics.head()):
Date Player Name Team Name Opposition \
0 2020-08-23 Kingsley Coman Bayern München Paris Saint Germain
1 2020-08-23 Joshua Kimmich Bayern München Paris Saint Germain
2 2020-08-23 Thiago Alcántara Bayern München Paris Saint Germain
3 2020-08-23 Manuel Neuer Bayern München Paris Saint Germain
4 2020-08-23 David Alaba Bayern München Paris Saint Germain
Home or Away? Competition Starting Lineup? Formation \
0 Away Champions League Lineup 4-2-3-1
1 Away Champions League Lineup 4-2-3-1
2 Away Champions League Lineup 4-2-3-1
3 Away Champions League Lineup 4-2-3-1
4 Away Champions League Lineup 4-2-3-1
Matchday Dividends FI Game Position ... Interceptions Blocks Clearances \
0 0.18 Forward ... 0.0 0.0 0.0
1 0.10 Midfielder ... 1.0 1.0 1.0
2 0.00 Midfielder ... 2.0 0.0 0.0
3 0.00 Goalkeeper ... 0.0 0.0 0.0
4 0.00 Defender ... 1.0 0.0 2.0
Offsides Fouls Committed Yellow Cards Red Card (Two Yellows) \
0 0.0 0.0 0.0 0.0
1 0.0 1.0 0.0 0.0
2 NaN 4.0 0.0 0.0
3 0.0 0.0 0.0 0.0
4 0.0 0.0 0.0 0.0
Straight Red Cards Goals Conceded (GKs) \
0 0.0 0.0
1 0.0 0.0
2 0.0 0.0
3 0.0 0.0
4 0.0 0.0
Different Game Position to Current FI Position?
0 1
1 1
2 0
3 0
4 0