gpt4 book ai didi

python - 如何为散点图上的每个点添加标签? Matplotlib

转载 作者:行者123 更新时间:2023-12-01 07:28:04 24 4
gpt4 key购买 nike

第一篇文章在这里。开始使用 python 中的 NFLScrapr 包,并尝试创建一个散点图来显示一些信息。现在散点图仅显示点,但我想知道是否有一种方法可以从相应的数据向每个图添加标签?

从这里开始

league_rushing_success = data.loc[(data['play_type']=='run') & (data['down']<=4)].groupby(by='posteam')[['epa','success','yards_gained']].mean()

尝试用这个来绘制

#Make x and y variables for success rate data
x = league_rushing_success['success'].values
y = league_rushing_success['epa'].values
types = league_rushing_success['posteam'].values

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

#Make a scatter plot with success rate data
ax.scatter(x, y,)

#Adding labels and text
ax.set_xlabel('Rush Success Rate', fontsize=14)
ax.set_ylabel('EPA', fontsize=14)
ax.set_title('Rush Success Rate and EPA', fontsize=18)
ax.text(.46, .39, 'Running Backs Dont Matter', fontsize=10, alpha=.7)

for i,type in enumerate(types):
x = x_coords[i]
y = y_coords[i]
plt.scatter(x, y, marker='x', color='red')
plt.text(x+0.3, y+0.3, type, fontsize=9)

我遇到的错误是

KeyError                                  Traceback (most recent call last)
//anaconda3/lib/python3.7/site-packages/pandas/core/indexes/base.py in get_loc(self, key, method, tolerance)
2656 try:
-> 2657 return self._engine.get_loc(key)
2658 except KeyError:

pandas/_libs/index.pyx in pandas._libs.index.IndexEngine.get_loc()

pandas/_libs/index.pyx in pandas._libs.index.IndexEngine.get_loc()

pandas/_libs/hashtable_class_helper.pxi in pandas._libs.hashtable.PyObjectHashTable.get_item()

pandas/_libs/hashtable_class_helper.pxi in pandas._libs.hashtable.PyObjectHashTable.get_item()

KeyError: 'posteam'

During handling of the above exception, another exception occurred:

KeyError Traceback (most recent call last)
<ipython-input-81-ebdc88aed0ac> in <module>
2 x = league_rushing_success['success'].values
3 y = league_rushing_success['epa'].values
----> 4 types = league_rushing_success['posteam'].values
5
6 fig, ax = plt.subplots(figsize=(10,10))

//anaconda3/lib/python3.7/site-packages/pandas/core/frame.py in __getitem__(self, key)
2925 if self.columns.nlevels > 1:
2926 return self._getitem_multilevel(key)
-> 2927 indexer = self.columns.get_loc(key)
2928 if is_integer(indexer):
2929 indexer = [indexer]

//anaconda3/lib/python3.7/site-packages/pandas/core/indexes/base.py in get_loc(self, key, method, tolerance)
2657 return self._engine.get_loc(key)
2658 except KeyError:
-> 2659 return self._engine.get_loc(self._maybe_cast_indexer(key))
2660 indexer = self.get_indexer([key], method=method, tolerance=tolerance)
2661 if indexer.ndim > 1 or indexer.size > 1:

pandas/_libs/index.pyx in pandas._libs.index.IndexEngine.get_loc()

pandas/_libs/index.pyx in pandas._libs.index.IndexEngine.get_loc()

pandas/_libs/hashtable_class_helper.pxi in pandas._libs.hashtable.PyObjectHashTable.get_item()

pandas/_libs/hashtable_class_helper.pxi in pandas._libs.hashtable.PyObjectHashTable.get_item()

KeyError: 'posteam'

enter image description here

最佳答案

此行会给您一个错误,因为 postteam 是一个索引:

# error
types = league_rushing_success['posteam'].values

# try this
types = league_rushing_success.reset_index()['posteam'].values
<小时/>

从您的代码中也不太清楚 x_coordsy_coords 的含义(并且也会给您一个错误):

for i,type in enumerate(types):
x = x_coords[i] # would give name 'x_coords' is not defined
y = y_coords[i] # 'y_coords' is not defined

如果您想向绘图添加标签,查看 matplotlib.pyplot.annotate 可能是个好主意。

# I would use something like this instead of `plt.text`
for i, txt in enumerate(types):
ax.annotate(txt, (x[i], y[i]), xytext=(10,10), textcoords='offset points')
plt.scatter(x, y, marker='x', color='red')
<小时/>

总结一下:

x = league_rushing_success['success'].values
y = league_rushing_success['epa'].values
types = league_rushing_success.reset_index()['posteam'].values

fig, ax = plt.subplots(figsize=(10,10))
ax.scatter(x, y)

ax.set_xlabel('Rush Success Rate', fontsize=14)
ax.set_ylabel('EPA', fontsize=14)
ax.set_title('Rush Success Rate and EPA', fontsize=18)

for i, txt in enumerate(types):
ax.annotate(txt, (x[i], y[i]), xytext=(10,10), textcoords='offset points')
plt.scatter(x, y, marker='x', color='red')

enter image description here

点是随机生成的,但情节的风格应该是相同的。

我希望这会有所帮助。

关于python - 如何为散点图上的每个点添加标签? Matplotlib,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57349722/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com