Python 在散点图中使用plt.text()时出错
我有这样一个数据帧:Python 在散点图中使用plt.text()时出错,python,pandas,matplotlib,Python,Pandas,Matplotlib,我有这样一个数据帧: batsman balls runs strike_rate 6's 4's Team Highest_score A Ashish Reddy 196 280 142.857143 16 15 DC 10 A Ashish Reddy 196 280 142.857143 16 15 SRH 36 A C
batsman balls runs strike_rate 6's 4's Team Highest_score
A Ashish Reddy 196 280 142.857143 16 15 DC 10
A Ashish Reddy 196 280 142.857143 16 15 SRH 36
A Chandila 7 4 57.142857 0 0 RR 4
A Chopra 75 53 70.666667 7 0 KKR 24...
我想做的是画一个散点图,这样我可以比较两个击球手。所以我写了一个函数:
batsman1='MS Dhoni'
batsman2='V Kohli'
def batsman_comparator():
sns.FacetGrid(balls,hue='Team',size=8).map(mlt.scatter, "runs", "strike_rate", alpha=0.5).add_legend()
bats1=balls[balls['batsman']==batsman1]
bats2=balls[balls['batsman']==batsman2]
mlt.scatter(bats1["runs"],bats1["strike_rate"],s=50,c='#55ff33')
mlt.text(bats1["runs"],bats1["strike_rate"],batsman1,
fontsize=10, weight='bold', color='#f46d43')
mlt.scatter(bats2["runs"],bats2["strike_rate"],s=50,c='#f73545')
mlt.text(bats2["runs"],bats2["strike_rate"], batsman2,
fontsize=10, weight='bold', color='#ff58fd')
mlt.show()
batsman_comparator()
好的,通过使用这个函数,我可以得到正确的曲线图。
但是,当我添加mlt.text()以显示击球手在特定点上的姓名时,我发现了一个错误:
TypeError: cannot convert the series to <class 'float'>
TypeError:无法将序列转换为
现在,在删除mlt.text()之后,函数工作正常。如何使用mlt.text()显示击球手的姓名。任何其他选择也可以。您似乎没有正确使用
mlt.text()
。请参阅Matplotlib文档。我相信text()
函数只能在一个点上应用。我想您可能正在寻找mlt.annotate()
。这个问题可能会有帮助