16

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

houdinisparks
  • 1,180
  • 1
  • 10
  • 16

2 Answers2

25

Put the thing in brackets:

plt.scatter([X[:,0]],[X[:,1]])
Dev-iL
  • 23,742
  • 7
  • 57
  • 99
a good guy
  • 266
  • 3
  • 2
9

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')
Jeeter
  • 5,887
  • 6
  • 44
  • 67