散点图来自Python中Matrix的数据

时间:2018-06-13 16:40:29

标签: python matplotlib

我的矩阵看起来像:

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

现在我想使用散点图来绘制它。其中:

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

例如。第一点是:

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

我已经尝试了以下

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由以下人员创建:

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 ...

有什么建议吗?

1 个答案:

答案 0 :(得分:1)

Matrix[:][0]为您提供数组的第一行。

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

假设Matrix是一个numpy数组,你需要像

一样索引它
Matrix[:,0]

获取第一列。如果它不是一个numpy数组,你可以通过Matrix = numpy.array(Matrix)将其设为一个。

因此,

plt.scatter(Matrix[:,0], Matrix[:,1], c=Matrix[:,2])