以下代码
plt.scatter(X[0, :], X[1, :], c=y, cmap=plt.cm.Spectral)
报错:
ValueError: RGBA sequence should have length 3 or 4
During handling of the above exception, another exception occurred:
......
ValueError: 'c' argument has 1 elements, which is not acceptable for use with 'x' with size 300, 'y' with size 300.
修改为:
import operator
from functools import reduce
plt.scatter(X[0, :], X[1, :], c=reduce(operator.add, y), cmap=plt.cm.Spectral)
即可解决
本文解决了一个关于Matplotlib散点图中颜色映射的问题,当尝试使用颜色参数c=y进行绘图时,由于'y'的元素数量与'x'和'y'坐标不匹配导致错误。通过使用reduce和operator.add函数,将'y'转换为正确的格式,从而解决了此问题。





