在 matplotlib 的散点图中注释每个数据点 python

Annotating every data point in scatter plot in matplotlib python

散点图没有在图中每个数据点旁边显示人名。

我正在尝试绘制工资和奖金的散点图。唯一缺少的是图中每个数据点的每个员工的姓名。

正在研究 TypeError: cannot concatenate 'str' and 'tuple' objects

fig, ax = plt.subplots()
my_scatter_plot = ax.scatter(
df["salary"], 
df["bonus"] 

)
ax.set_xlabel("Salary")
ax.set_ylabel("Bonus")
ax.set_title("Enron Employees Salary and Bonus Scatter Plot")

for _, row in df[["Names","salary","bonus"]].iterrows():
    xy = row[["salary", "bonus"]]
    xytext= xy + (0.02, 5)
    ax.annotate(row["Names"], xy, xytext)


plt.show() 


TypeError: cannot concatenate 'str' and 'tuple' objects

希望看到员工对应的每条数据的名称。

我认为问题出在您的 "salary""bonus" 列被解释为字符串这一事实。因此,当您 xy + (0.02, 5) 时,它认为您正在尝试将字符串 (xy) 与元组连接起来。我认为你应该根据你的情况尝试将这些列转换为浮点数或整数。