2016-10-25 353 views
0

我試圖在二維空間中繪製點,插入標籤和每個點的顏色設置在哪裏(不依賴於標籤)。 第一次嘗試,不考慮標籤是:pylab:用顏色繪製點

import pylab 
x = [-0.01611772, 1.51755901, -0.64869352, -1.80850313, -0.11505037] 
y = [ 0.04845168, -0.45576903, 0.62703651, -0.24415787, -0.41307092] 
colors = ['b', 'g', 'r', 'b', 'r'] 

for k in range(len(x)): 
    pylab.scatter(x[k],y[k],colors[k]) 

它返回一個沒有必要的操作適合於執行情節的錯誤:具有思想

Traceback (most recent call last): 
    File "<pyshell#1>", line 12, in <module> 
    pylab.scatter(x[k],y[k],colors[k]) 
    File "C:\Program Files\Anaconda\lib\site-packages\matplotlib\pyplot.py", line 3258, in scatter 
    edgecolors=edgecolors, data=data, **kwargs) 
    File "C:\Program Files\Anaconda\lib\site-packages\matplotlib\__init__.py", line 1818, in inner 
    return func(ax, *args, **kwargs) 
    File "C:\Program Files\Anaconda\lib\site-packages\matplotlib\axes\_axes.py", line 3866, in scatter 
    alpha=alpha 
    File "C:\Program Files\Anaconda\lib\site-packages\matplotlib\collections.py", line 833, in __init__ 
    self.set_sizes(sizes) 
    File "C:\Program Files\Anaconda\lib\site-packages\matplotlib\collections.py", line 806, in set_sizes 
    scale = np.sqrt(self._sizes) * dpi/72.0 * self._factor 
TypeError: ufunc 'sqrt' not supported for the input types, and the inputs could not be safely coerced to any supported types according to the casting rule ''safe'' 

有人嗎?

回答

2

這裏沒有必要使用for循環。只需通過您的顏色列表到pylab.scatter功能:

import pylab 

x = [-0.01611772, 1.51755901, -0.64869352, -1.80850313, -0.11505037] 
y = [ 0.04845168, -0.45576903, 0.62703651, -0.24415787, -0.41307092] 

colors = ['b', 'g', 'r', 'b', 'r'] 

pylab.scatter(x, y, c=colors) 

pylab.show() 

這將產生以下圖表:

enter image description here

+0

我不知道爲什麼在它不工作....解決!謝謝! –

0

變化

pylab.scatter(x[k],y[k],colors[k]) 

pylab.scatter(x = x[k],y = y[k],c = colors[k]) 
+0

這對已經接受的答案沒有任何改進。 – yacc

+0

好的。我只想指出,使用for循環很好,以防當前答案中不明顯。 – hakunamatata