美文网首页
pandas异常-'c' argument must eithe

pandas异常-'c' argument must eithe

作者: 橘猫吃不胖 | 来源:发表于2020-05-14 15:52 被阅读0次

记录一个错误,关于plot.scatter的,在绘制散点图的时候出现的

这个错误困惑了我好久,后来,仔细看了文档,才发现为啥。

官网中有个例子:

df = pd.DataFrame([[5.1, 3.5, 0], [4.9, 3.0, 0], [7.0, 3.2, 1],
                   [6.4, 3.2, 1], [5.9, 3.0, 2]],
                  columns=['length', 'width', 'species'])
ax2 = df.plot.scatter(x='length',
                      y='width',
                      c='species',
                      colormap='viridis')

好的,到这里都没有问题,问题,就出在这个对于这个c的理解

就是这个,我没仔细看,以为根据传入的column name,就可以自动作为一个维度进行分类了,切换不同的颜色,

所以,当我把数据集改为下面这样的时候,我以为也是可以的:

df = pd.DataFrame([[5.1, 3.5, 'a'], [4.9, 3.0, 'a'], [7.0, 3.2, 'b'],
                   [6.4, 3.2, 'c'], [5.9, 3.0, 'c']],
                  columns=['length', 'width', 'species'])

就是species不是数字,变成字符串了,我想当然的以为,它可以自动去分配颜色,结果就报错了,上面一开始的错误,再仔细看了文档后才发现,

这里传入的是一个位置,是position,用来和colormap的颜色对应的,所以传入字符串是不行的,不行的,注意一下

相关文章

网友评论

      本文标题:pandas异常-'c' argument must eithe

      本文链接:https://www.haomeiwen.com/subject/baitohtx.html