python - 在 Python 3.5.1 中使用 pylab.scatter 和 cmap

标签 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]), 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

我该如何解决这个问题?

最佳答案

问题是 map does not return a list in Python 3 .您可以将 map 传递给 list 或使用列表理解,这实际上比您的 lambda 更短:

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

一个更短的版本完全摆脱了 map :

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

关于python - 在 Python 3.5.1 中使用 pylab.scatter 和 cmap,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37244655/

相关文章:

python - 需要帮助了解 Restful API 中的 SQL 注入(inject)漏洞

python - 引发TimeoutException(消息,屏幕,堆栈跟踪)selenium.common.exceptions.TimeoutException : Message: Assertion failed

python - String to Bytes Python 不改变编码

python - 从列范围返回新数据框( Pandas )

python - 关于 Github OAuth 身份验证

python - Pandas 中的多索引排序

python - python 字典中部分匹配键的最大值

python - 如何在 Python 2.7 中加载 Python 3 pickle *不改变它*?

python-2.7 - 403 "Request had insufficient authentication scopes"使用 Python 在 Google API 中创建草稿时

python - 将QMessageBox系统图标添加到QDialog中