Reputation: 135
I have extracted some table data using the code below:
import pandas as pd
import matplotlib.pyplot as plt
stat_dict={'Disposals' : 0,
'Kicks' : 1,
'Marks' : 2,
'Handballs' : 3,
'Goals' : 4,
'Behinds' : 5,
'Hitouts' : 6,
'Tackles' : 7,
'Rebounds' : 8,
'Inside50s' : 9,
'Clearances': 9,
'Clangers' : 10,
'FreesFor' : 11,
'FreesAgainst' : 12,
'ContestedPosessions' : 13,
'UncontestedPosesseions' : 14,
'ContestedMarks' : 15,
'MarksInside50' : 16,
'OnePercenters' : 17,
'Bounces' : 18,
'GoalAssists' : 19,
'Timeplayed' : 20}
team_lower_case='fremantle'
player="Fyfe, Nat"
stat_required='Disposals'
rounds=7
tables = pd.read_html("https://afltables.com/afl/stats/teams/"
+str(team_lower_case)+"/2018_gbg.html")
for df in tables:
df.drop(df.columns[rounds+1:], axis=1, inplace=True) # remove
unwanted columns
df.columns = df.columns.droplevel(0) # remove extra index level
stat_table=tables[stat_dict[stat_required]]
player_stat=stat_table[stat_table["Player"]==player]
player_stat=player_stat.iloc[:,1:8]
Which outputs this:
R1 R2 R3 R4 R5 R6 R7
8 22.0 29.0 38.0 25.0 43.0 27.0 33.0
I then plot this data:
plt.scatter(range(1,rounds+1),player_stat)
plt.show()

I want to set the y axis limit to zero:
plt.ylim(0,50)
But it results in the plot being all squashed and weird:
I've tried the following as well:
plt.ylim(ymin=0)
Upvotes: 0
Views: 1237
Reputation: 25400
Your data are strings. The problem can be reproduced using the following:
y = ["22.0", "29.0", "38.0", "25.0", "43.0", "27.0", "33.0"]
plt.scatter(range(len(y)),y)
plt.ylim(0,50)
You need to convert player_stat
to a numeric data type. One possible way to do this would be using .astype(float)
. For your example it would be something like:
plt.scatter(range(1,rounds+1), player_stat.astype(float))
Upvotes: 2