python中矩阵的散点图数据
我有一个矩阵,看起来像:python中矩阵的散点图数据,python,matplotlib,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[:][
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
我的矩阵由以下人员创建:
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 ...
有什么建议吗
矩阵[:][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])
你到底有什么困难?你试过什么?您已经知道多少
matplotlib
?这个问题需要更多的背景,尤其是你已经做过的一些工作。plt.scatter(矩阵[0],矩阵[1],c=Matrix[2])
?@HarvIpan这是我第一次尝试。我没有为我工作,我的散点图中只有4个条目。但我的矩阵有100多行。@ChrizZlyBear,你检查过重叠吗?@HarvIpan是的。我添加了一些信息和一种(低效的)部分有效的方法。也许这些信息会有帮助,我自己创建了一个矩阵。现在我切换到numpy,它更清晰、更快速(而且很有效)。谢谢你能告诉我怎样把颜色改成蓝色和黄色吗?我有黑色和黄色。如果matplotlib的产品都不适合您,您可以使用您自己的颜色映射,如所述,例如。