0
votes

The scatter plot is not showing the name of the person next to every data point in the plot.

I am trying to draw a scatter plot of salary and bonus. The only thing that is missing is the name of each employee at every data point in the plot.

Reeving a 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

Expecting to see name of every data that correspond to the employee.

1
Closing the question. Several duplicates exist on this topic - Sheldore
Tried the stackoverflow.com/questions/14432557/… but no luck -- First converted bonus, salary, and names into a list , as the data type in the suggested solution were lists, but got this error ValueError: cannot convert float NaN to integer - Rizwan Ahmed

1 Answers

1
votes

I think the problem comes from the fact that your "salary" and "bonus" columns are interpreted as strings. Hence when you xy + (0.02, 5), it thinks that you are trying to concatenate the string (xy) with a tuple. I think you should try and cast those columns to floats or integers depending on your case.