简体   繁体   English

尝试使用 for 循环绘制 matplotlib 散点图时出现错误?

[英]I am getting an error when trying to plot a matplotlib scatter plot using for loop?

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我的极性和主观性 cols 是数值

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!不知道我在这里缺少什么,感谢任何帮助,谢谢!

df['polarity'][i] extract item at index i of the series df['polarity'] . df['polarity'][i]df['polarity']系列的索引i处提取项目。 The error says df['polarity'] does not have an index 3 , for example, df can look like错误说df['polarity']没有索引3 ,例如, df看起来像

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 尝试绘制散点图时,出现“ ValueError:x和y必须大小相同”错误, - I am getting an error as 'ValueError: x and y must be the same size' when trying to plot a scatter plot 我正在尝试使用 matplotlib 将 plot 散点图放到 python 中的 3d 连续图上 - I am trying to plot a scatter graph onto my 3d continuous graph in python using matplotlib 尝试在matplotlib中制作散点图时出现“类型错误:未对此类型实现” - 'Type Error: Not implemented for this type' when trying to make a Scatter plot in matplotlib 尝试使用 matplotlib 进行 plot 时,我不断收到错误消息 - I keep getting an error when trying to plot with matplotlib Python错误:使用matplotlib生成散点图 - Python error: generating a scatter plot using matplotlib 具有未知错误的Matplotlib散点图 - Matplotlib scatter plot with unknown error 尝试使用matplotlib绘制大型数组时出现内存错误 - Getting Memory Error when trying to plot large array with matplotlib 尝试使用python(matplotlib和numpy)绘制函数,如果我稍加改动就会遇到“值错误”和“类型错误” - Trying to plot a function using python (matplotlib and numpy) and am running into a Value Errors and a Type Error if I alter it slightly 使用 matplotlib 绘制散点图和曲线图 - Scatter and curve plot using matplotlib 使用matplotlib进行散点图 - using matplotlib to do a scatter plot
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM