2016-05-15 22 views
3

當我使用Python2,我可以繪製圖表使用pylab.scatter和CMAP在Python 3.5.1

from sklearn import datasets 
from matplotlib.colors import ListedColormap 
circles = datasets.make_circles() 

colors = ListedColormap(['red', 'blue']) 
pyplot.figure(figsize(8, 8)) 

pyplot.scatter(map(lambda x: x[0], circles[0]), map(lambda x: x[1], circles[0]), c = circles[1], cmap = colors) 

但是當我使用Python3,我不能這樣做。我試圖改變顏色,但不能。

我收到許多錯誤:

ValueError: length of rgba sequence should be either 3 or 4 

During handling of the above exception, another exception occurred: 

ValueError: to_rgba: Invalid rgba arg "[0 0 0 0 0 0 0 0 1 0 0 0 0 0 1 1 0 0 1 1 1 0 1 0 0 1 1 0 1 0 0 0 0 1 0 0 1 0 1 1 1 0 1 0 1 0 1 0 1 1 1 1 1 1 0 0 1 0 1 1 1 1 1 1 1 0 0 0 1 1 1 1 0 1 1 0 0 0 1 1 0 1 1 0 1 0 1 1 0 1 0 1 0 1 1 0 0 0 0 1]" 
length of rgba sequence should be either 3 or 4 

During handling of the above exception, another exception occurred: 

ValueError: Color array must be two-dimensional 

我該如何解決這個問題?

+0

什麼的'版本matplotlib'你使用Py2和Py3? – MattDMo

+0

好吧,如果它讓你感覺更好,我會在Linux上獲得與matplotlib 1.5.1和Python 2.7.11和3.5.1相同的結果。奇怪... – MattDMo

+0

1)Anaconda + Python 2.7.1 + Matplotlib 1.5.1 - 一切正常 2)Anaconda + Python 3.5.1 + Matplotlib 1.5.1 - 全部不好 –

回答

3

問題是map does not return a list in Python 3。您可以通過maplist或使用列表理解,這實際上是比你的拉姆達短:

pyplot.scatter([x[0] for x in circles[0]], 
       [x[1] for x in circles[0]], c=circles[1], cmap=colors) 

更短的版本擺脫了地圖完全的:

pyplot.scatter(*zip(*circles[0]), c=circles[1], cmap=colors) 
+0

我完全忘記了'map' -list Py3中的東西。我腦海中的某些東西昨晚正在竊聽我關於「地圖」的情況,但我無法把它放在手指上。好答案。 – MattDMo