python 3 scatter plot gives "ValueError: Masked arrays must be 1-D" even though i am not using any masked array
python 3 scatter plot gives "ValueError: Masked arrays must be 1-D" even though i am not using any masked array
我正在尝试使用下面的 .scatter 方法绘制散点图。这里
ax.scatter(X[:,0], X[:,1], c = colors, marker = 'o', s=80, edgecolors = 'none')
与下面的 input/args 类:
X[:,0]] type: <class 'numpy.matrixlib.defmatrix.matrix'>
X[:,1]] type: <class 'numpy.matrixlib.defmatrix.matrix'>
colors type: <class 'list'>
然而 python 抛出值错误,如下所示:
error image
把东西放在括号里:
plt.scatter([X[:,0]],[X[:,1]])
我的经验是因为你的 X
是一个 numpy matrix
。
本质上,每当您尝试从一个矩阵中分离出一行时,它 returns 是另一个矩阵。 Numpy 似乎有一个约束,即矩阵 必须 是二维的,所以它不能说它是一维数组,也不能屏蔽它(因此 Masked arrays must be 1-D
错误)
我的解决方案是简单地 "cast" X
到 numpy.array
做:
X = np.array(X)
ax.scatter(X[:,0], X[:,1], c = colors, marker = 'o', s=80, edgecolors = 'none')
我正在尝试使用下面的 .scatter 方法绘制散点图。这里
ax.scatter(X[:,0], X[:,1], c = colors, marker = 'o', s=80, edgecolors = 'none')
与下面的 input/args 类:
X[:,0]] type: <class 'numpy.matrixlib.defmatrix.matrix'>
X[:,1]] type: <class 'numpy.matrixlib.defmatrix.matrix'>
colors type: <class 'list'>
然而 python 抛出值错误,如下所示: error image
把东西放在括号里:
plt.scatter([X[:,0]],[X[:,1]])
我的经验是因为你的 X
是一个 numpy matrix
。
本质上,每当您尝试从一个矩阵中分离出一行时,它 returns 是另一个矩阵。 Numpy 似乎有一个约束,即矩阵 必须 是二维的,所以它不能说它是一维数组,也不能屏蔽它(因此 Masked arrays must be 1-D
错误)
我的解决方案是简单地 "cast" X
到 numpy.array
做:
X = np.array(X)
ax.scatter(X[:,0], X[:,1], c = colors, marker = 'o', s=80, edgecolors = 'none')