程序调用matplotlib.pyplot.scatter绘图,结果报错,报错的具体信息如下:
Traceback (most recent call last):
File "F:\Program Files\Python35\lib\site-packages\matplotlib\colors.py", line 143, in to_rgba
rgba = _colors_full_map.cache[c, alpha]
KeyError: (0.0, None)
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "F:\Program Files\Python35\lib\site-packages\matplotlib\colors.py", line 195, in _to_rgba_no_colorcycle
c = map(float, c)
TypeError: 'float' object is not iterable
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "tsne.py", line 219, in
pyplot.scatter(X[:, 0], X[:,1], s=20, c=labels, alpha=0.5)
File "F:\Program Files\Python35\lib\site-packages\matplotlib\pyplot.py", line 3435, in scatter
edgecolors=edgecolors, data=data, **kwargs)
File "F:\Program Files\Python35\lib\site-packages\matplotlib_ init _.py", line 1892, in inner
return func(ax, *args, **kwargs)
File "F:\Program Files\Python35\lib\site-packages\matplotlib\axes_axes.py", line 4028, in scatter
alpha=alpha
File "F:\Program Files\Python35\lib\site-packages\matplotlib\collections.py", line 890, in init
Collection. init (self, **kwargs)
File "F:\Program Files\Python35\lib\site-packages\matplotlib\collections.py", line 139, in init
self.set_facecolor(facecolors)
File "F:\Program Files\Python35\lib\site-packages\matplotlib\collections.py", line 674, in set_facecolor
self._set_facecolor(c)
File "F:\Program Files\Python35\lib\site-packages\matplotlib\collections.py", line 659, in _set_facecolor
self._facecolors = mcolors.to_rgba_array(c, self._alpha)
File "F:\Program Files\Python35\lib\site-packages\matplotlib\colors.py", line 241, in to_rgba_array
result[i] = to_rgba(cc, alpha)
File "F:\Program Files\Python35\lib\site-packages\matplotlib\colors.py", line 145, in to_rgba
rgba = _to_rgba_no_colorcycle(c, alpha)
File "F:\Program Files\Python35\lib\site-packages\matplotlib\colors.py", line 198, in _to_rgba_no_colorcycle
raise ValueError("Invalid RGBA argument: {!r}".format(orig_c))
ValueError: Invalid RGBA argument: 0.0
总的来说就是下面三个错误
KeyError: (0.0, None)
TypeError: 'float' object is not iterable
ValueError: Invalid RGBA argument: 0.0
网上搜索了几个解决办法都不是我问题对应的,最终的解决办法:
问题出在scatter的调用
pyplot.scatter(X[:, 0], X[:,1], s=20, c=labels, alpha=0.5)
其实想做的是X提供数据的二维坐标,然后labels数据提供绘图的颜色
但是发现X的长度与labels的长度不符合,比如X是一个10309 2的向量,而labels是10320 1的向量,这样维度的不同,导致了调用的错误,因此最好检查下想,sactter调用的二维参数的长度,并且检查下他们与c参数长度是否匹配,例如:
assert(len(x) == len(y))
assert(len(x) == len(colors))
scatter(x, y, s=20, c=colors)