-1

I have a matrix looking like:

50    3    1
100   3    1
150   3    0
...
100   15   0
150   15   0

Now I want to use a scatter plot to plot it. Where:

Matrix[:][0] = X-Values  #  For demonstration used the ':' operator from Matlab which means it includes all values.
Matrix[:][1] = Y-Values
Matrix[:][2] = color where 0 = blue & 1 = yellow

For example. The first point would be:

Matrix[0][0] = 50 as X-Value of first point
Matrix[0][1] = 3 as Y-Value of first point
Matrix[0][2] = 1 as color (yellow) of first point

I already tried the following

plt.scatter(Matrix[0], Matrix[1]) # didn't worked. Had only like 4 scatter points instead of over 100
plt.scatter(Matrix[:][0], Matrix[:][1]) # Same issue
for i in range(len(Matrix)):
    plt.scatter(Matrix[i][0], Matrix[i][1], c=Matrix[i][2]) # worked, but is pretty slow and all points were black instead of colored

My Matrix is created by:

w, h = len(list_of_dfs), 3) 
Matrix = [[0 for x in range(h)] for y in range(w)]
# And then filled like
Matrix[0][0] = 50 ...

Any suggestions?

ChrizZlyBear
  • 121
  • 2
  • 11

1 Answers1

1

Matrix[:][0] gives you the first row of the array.

Matrix[:][0] == (Matrix[:])[0] == Matrix[0] 

Assuming that Matrix is a numpy array, you need to index it like

Matrix[:,0]

to get the first column out. If it isn't a numpy array, you can make it one via Matrix = numpy.array(Matrix).

Hence,

plt.scatter(Matrix[:,0], Matrix[:,1], c=Matrix[:,2])
ImportanceOfBeingErnest
  • 321,279
  • 53
  • 665
  • 712
  • I had a matrix created by myself. Now I switched to numpy, which is clearer and faster (and it works). Thanks. Could you aswell tell me how I can change the color to blue and yellow? I get black and yellow. – ChrizZlyBear Jun 13 '18 at 17:15
  • In case none of [the colormaps](https://matplotlib.org/examples/color/colormaps_reference.html) that ship with matplotlib works for you you may use your own colormap as described e.g. [here](https://stackoverflow.com/a/46778420/4124317). – ImportanceOfBeingErnest Jun 13 '18 at 17:19