matplotlib.scatter()不适用于Python 3.6上的Numpy
我很难理解为什么matplotlib.scatter()在使用Python 3.6.3作为解释器时一直抛出以下异常,但在使用MacBook内置的2.7时效果很好:matplotlib.scatter()不适用于Python 3.6上的Numpy,python,numpy,matplotlib,Python,Numpy,Matplotlib,我很难理解为什么matplotlib.scatter()在使用Python 3.6.3作为解释器时一直抛出以下异常,但在使用MacBook内置的2.7时效果很好: Traceback (most recent call last): File "/Users/thomastiotto/python_envs/MachineLearning/lib/python3.6/site-packages/matplotlib/colors.py", line 132, in to_rgba r
Traceback (most recent call last):
File "/Users/thomastiotto/python_envs/MachineLearning/lib/python3.6/site-packages/matplotlib/colors.py", line 132, in to_rgba
rgba = _colors_full_map.cache[c, alpha]
TypeError: unhashable type: 'numpy.ndarray'
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "/Users/thomastiotto/python_envs/MachineLearning/lib/python3.6/site-packages/matplotlib/axes/_axes.py", line 4050, in scatter
colors = mcolors.to_rgba_array(c)
File "/Users/thomastiotto/python_envs/MachineLearning/lib/python3.6/site-packages/matplotlib/colors.py", line 233, in to_rgba_array
result[i] = to_rgba(cc, alpha)
File "/Users/thomastiotto/python_envs/MachineLearning/lib/python3.6/site-packages/matplotlib/colors.py", line 134, in to_rgba
rgba = _to_rgba_no_colorcycle(c, alpha)
File "/Users/thomastiotto/python_envs/MachineLearning/lib/python3.6/site-packages/matplotlib/colors.py", line 189, in _to_rgba_no_colorcycle
raise ValueError("RGBA sequence should have length 3 or 4")
ValueError: RGBA sequence should have length 3 or 4
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "/Users/thomastiotto/Documents/USI/1 semester/Machine Learning/Assignments/Assignment 1/skeleton.py", line 458, in <module>
main()
File "/Users/thomastiotto/Documents/USI/1 semester/Machine Learning/Assignments/Assignment 1/skeleton.py", line 455, in main
run_part1()
File "/Users/thomastiotto/Documents/USI/1 semester/Machine Learning/Assignments/Assignment 1/skeleton.py", line 156, in run_part1
plot_boundary(p, X, T)
File "/Users/thomastiotto/Documents/USI/1 semester/Machine Learning/Assignments/Assignment 1/skeleton.py", line 142, in plot_boundary
plot_data(X, targets)
File "/Users/thomastiotto/Documents/USI/1 semester/Machine Learning/Assignments/Assignment 1/skeleton.py", line 129, in plot_data
plt.scatter(X[:, 0], X[:, 1], s=40, c=T, cmap=plt.cm.Spectral)
File "/Users/thomastiotto/python_envs/MachineLearning/lib/python3.6/site-packages/matplotlib/pyplot.py", line 3357, in scatter
edgecolors=edgecolors, data=data, **kwargs)
File "/Users/thomastiotto/python_envs/MachineLearning/lib/python3.6/site-packages/matplotlib/__init__.py", line 1710, in inner
return func(ax, *args, **kwargs)
File "/Users/thomastiotto/python_envs/MachineLearning/lib/python3.6/site-packages/matplotlib/axes/_axes.py", line 4055, in scatter
raise ValueError(msg.format(c.shape, x.size, y.size))
ValueError: c of shape (11, 1) not acceptable as a color sequence for x with size 11, y with size 11
其中X是一个[11x2]Numpy数组
如果我将解释器设置为MacOS上的内置Python 2.7,代码运行良好,将其设置为Python 3.6.2或3.6.3会导致上述错误。Matplotlib版本在前一种情况下为1.3.1,在后一种情况下为2.1
有什么想法吗?c需要一个一维数组 拉威尔(T.ravel)应该能做到这一点
plt.scatter(X[:, 0], X[:, 1], s=40, c=T, cmap=plt.cm.Spectral)
在这个函数中,c需要一维数组,如上面的回答中所述,使用T.ravel或T.reformate(400,)使用np。reformate:
import numpy as np
t1 = np.array([[1,2,3,4,5,6] , [7,8,9,10,11,12]])
t1_single = np.reshape(t1, -1)
print(t1.shape)
print(t1_single.shape)
输出:(2,6)
(12,)
您还可以使用
c=np.squence(T)
我认为这里的问题实际上是一个更大的python/numpy问题的一部分,即无法推断1D数组的正确用法。这浪费了大量的编码和调试时间。11x1矩阵和11向量之间存在差异。使用
c=T[:,0]
。完美!不知道为什么它在较旧版本的软件包中工作,而不是在最新版本中。我在Python 2.6.2和Matplotlib 2.1中遇到了完全相同的问题
plt.scatter(X[:, 0], X[:, 1], s=40, c=T, cmap=plt.cm.Spectral)
import numpy as np
t1 = np.array([[1,2,3,4,5,6] , [7,8,9,10,11,12]])
t1_single = np.reshape(t1, -1)
print(t1.shape)
print(t1_single.shape)