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!

Chris90
  • 1,868
  • 5
  • 20
  • 42
  • Try to look at this [answer](https://stackoverflow.com/a/45229426/8973620) – Mykola Zotko Sep 25 '20 at 19:53
  • Does this answer your question? [How to make several plots on a single page using matplotlib?](https://stackoverflow.com/questions/1358977/how-to-make-several-plots-on-a-single-page-using-matplotlib) – Mykola Zotko Sep 25 '20 at 19:54

1 Answers1

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')
Quang Hoang
  • 146,074
  • 10
  • 56
  • 74