【问题标题】:I am getting an error when trying to plot a matplotlib scatter plot using for loop?尝试使用 for 循环绘制 matplotlib 散点图时出现错误?
【发布时间】:2020-09-25 19:46:02
【问题描述】:

当我尝试在下面绘制散点图时,我遇到了一个我似乎无法理解的错误:

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()

我的极性和主观性列是数值

我明白了

KeyError:3  

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

不确定我在这里缺少什么,感谢任何帮助,谢谢!

【问题讨论】:

标签: python python-3.x pandas matplotlib


【解决方案1】:

df['polarity'][i] 提取系列df['polarity'] 的索引i 处的项目。错误说df['polarity'] 没有索引3,例如df 可以看起来像

   polarity
0         1
1         2
2         3
4         1

你为什么不试试:

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

或者:

df.plot.scatter(x='polarity', y='subjectivity')

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-09-25
    • 2020-12-27
    • 1970-01-01
    • 2015-06-07
    • 1970-01-01
    • 2012-11-22
    • 2018-06-23
    相关资源
    最近更新 更多