程序調(diào)用matplotlib.pyplot.scatter繪圖,結(jié)果報錯,報錯的具體信息如下:
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
網(wǎng)上搜索了幾個解決辦法都不是我問題對應(yīng)的,最終的解決辦法:
問題出在scatter的調(diào)用
pyplot.scatter(X[:, 0], X[:,1], s=20, c=labels, alpha=0.5)
其實想做的是X提供數(shù)據(jù)的二維坐標(biāo),然后labels數(shù)據(jù)提供繪圖的顏色
但是發(fā)現(xiàn)X的長度與labels的長度不符合,比如X是一個103092的向量,而labels是103201的向量,這樣維度的不同,導(dǎo)致了調(diào)用的錯誤,因此最好檢查下想,sactter調(diào)用的二維參數(shù)的長度,并且檢查下他們與c參數(shù)長度是否匹配,例如:
assert(len(x) == len(y))
assert(len(x) == len(colors))
scatter(x, y, s=20, c=colors)