在Python 3.5.1中使用pylab.scatter和cmap
当我使用Python2时,我可以绘制图形在Python 3.5.1中使用pylab.scatter和cmap,python,python-2.7,python-3.x,matplotlib,Python,Python 2.7,Python 3.x,Matplotlib,当我使用Python2时,我可以绘制图形 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]), m
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
如何修复此问题?问题在于。您可以将映射
传递到列表
,也可以使用列表理解,它实际上比lambda短:
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)
您在Py2和Py3中使用的是哪个版本的
matplotlib
?好吧,如果它让您感觉更好的话,我在Linux上使用matplotlib 1.5.1和Python 2.7.11以及3.5.1也会得到相同的结果。奇怪…1)Anaconda+Python 2.7.1+Matplotlib 1.5.1-一切正常2)Anaconda+Python 3.5.1+Matplotlib 1.5.1-所有badI完全忘记了Py3中的映射
-列表。昨晚我脑子里有什么东西在烦我关于地图的事,但我无法确定。回答得好。