1

I am getting an error I cant seem to understand when im trying to plot a scatterplot below:

plt.figure(figsize=(8,6))
for i in range(0,df.shape[0]):
    plt.scatter(df['polarity'][i], df['subjectivity'][i], color = 'Blue' )

plt.title('Sentiment Analysis')
plt.xlabel('Polarity')
plt.ylabel('Subjectivity')
plt.show()

Where my polarity and subjectivity cols are number values

I get

KeyError:3  

 ----> 3     plt.scatter(df['polarity'][i], df['subjectivity'][i], color = 'Blue' )

not sure what I am missing here, any help appreciated, thanks!

2

1 Answer 1

1

df['polarity'][i] extract item at index i of the series df['polarity']. The error says df['polarity'] does not have an index 3, for example, df can look like

   polarity
0         1
1         2
2         3
4         1

Why don't you try:

plt.scatter(df['polarity'], df['subjectivity'], color='b')

Or:

df.plot.scatter(x='polarity', y='subjectivity')
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.