Python Matplotlib.pyplot散点:将ylim min设置为零不起作用
我使用以下代码提取了一些表格数据:Python Matplotlib.pyplot散点:将ylim min设置为零不起作用,python,matplotlib,Python,Matplotlib,我使用以下代码提取了一些表格数据: import pandas as pd import matplotlib.pyplot as plt stat_dict={'Disposals' : 0, 'Kicks' : 1, 'Marks' : 2, 'Handballs' : 3, 'Goals' : 4, 'Behinds' : 5, 'Hitouts'
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]
哪个输出:
R1 R2 R3 R4 R5 R6 R7
8 22.0 29.0 38.0 25.0 43.0 27.0 33.0
然后,我绘制这些数据:
plt.scatter(range(1,rounds+1),player_stat)
plt.show()

我想将y轴限制设置为零:
plt.ylim(0,50)
但这会导致情节被压扁和怪异:
我也尝试过以下方法:
plt.ylim(ymin=0)
您的数据是字符串。可以使用以下方法重现该问题:
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)
您需要将player\u stat
转换为数字数据类型。一种可能的方法是使用.astype(float)
。对于您的示例,它将类似于:
plt.scatter(range(1,rounds+1), player_stat.astype(float))
您的数据是字符串。可以使用以下方法重现该问题:
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)
您需要将player\u stat
转换为数字数据类型。一种可能的方法是使用.astype(float)
。对于您的示例,它将类似于:
plt.scatter(range(1,rounds+1), player_stat.astype(float))
当我使用
y=[22.0,29.0,38.0,25.0,43.0,27.0,33.0];plt.散射(范围(len(y)),y);plt.ylim(0,50)
-它适合我(不是所有的人都挤在一起)。你使用的是什么版本?玩家统计的类型和数据类型是什么?@wwii我尝试了你的代码,它确实按预期工作。也许我对数据本身有问题?我正在使用Python 3。6@xg.plt.pyplayer_stat是一个数据帧。我不知道你说的数据类型是什么意思?看起来你的数据是字符串。当我使用y=[22.0,29.0,38.0,25.0,43.0,27.0,33.0]时,您可能希望将它们转换为整数;plt.散射(范围(len(y)),y);plt.ylim(0,50)
-它适合我(不是所有的人都挤在一起)。你使用的是什么版本?玩家统计的类型和数据类型是什么?@wwii我尝试了你的代码,它确实按预期工作。也许我对数据本身有问题?我正在使用Python 3。6@xg.plt.pyplayer_stat是一个数据帧。我不知道你说的数据类型是什么意思?看起来你的数据是字符串。您可能想将它们转换为整数。我同意数据是字符串,但按照您的建议也不行。结果与我文章的第三个例子相同。我同意数据是字符串,但是按照你的建议也不行。结果与我文章的第三个例子相同。