Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

python 3 scatter plot gives "ValueError: Masked arrays must be 1-D" even though i am not using any masked array

I am trying to plot a scatter plot using the .scatter method below. Here

ax.scatter(X[:,0], X[:,1], c = colors, marker = 'o', s=80, edgecolors = 'none')

with the input/args classes below:

X[:,0]] type: <class 'numpy.matrixlib.defmatrix.matrix'> X[:,1]] type: <class 'numpy.matrixlib.defmatrix.matrix'> colors type: <class 'list'>

however python is throwing a value error as seen here: error image

like image 285
houdinisparks Avatar asked May 28 '17 05:05

houdinisparks


2 Answers

Put the thing in brackets:

plt.scatter([X[:,0]],[X[:,1]])
like image 71
a good guy Avatar answered Nov 02 '22 03:11

a good guy


My experience with this is because your X is a numpy matrix.

Essentially, whenever you try and isolate a row from a matrix, it returns another matrix. Numpy seems to have a constraint that matrices must be 2 dimensional, so it can't tell that it's a 1-d array, and can't mask it (hence the Masked arrays must be 1-D error)

The solution for me was to simply "cast" X to a numpy.array by doing:

X = np.array(X)
ax.scatter(X[:,0], X[:,1], c = colors, marker = 'o', s=80, edgecolors = 'none')
like image 32
Jeeter Avatar answered Nov 02 '22 02:11

Jeeter