散点图的矩阵元素

时间:2016-04-23 19:00:22

标签: python numpy matrix plot scatter-plot

我有这个数组:

b=np.array([1,2,3])

这个矩阵:

a=np.array([[ 4,  2, 12],
   [ 7,  12,  0],
   [ 10,  7, 10]])

我现在想要创建一个散点图,它将b [i]作为x轴,将[j] [i]作为y轴。更具体一点,我希望我的情节中的点/坐标为:

(b[i],a[j][i]) 

在我的情况下将是:

(1,4) (1,7) (1,10) (2,2) (2,12) (2,7) (3,12) (3,0) (3,10)

然后我可以轻松地绘制。情节看起来像这样:

Scatter Plot

任何人都可以帮助我为我的情节创造积分吗?有一般解决方案吗?

2 个答案:

答案 0 :(得分:1)

import matplotlib.pyplot as p
import numpy as np


b=np.array([1,2,3])
a=np.array([[ 4,  2, 12],
   [ 7,  12,  0],
   [ 10,  7, 10]])

p.plot(b,a[0],'o-')# gives you different colors for different datasets
p.plot(b,a[1],'o-')# showing you things that scatter won't
p.plot(b,a[2],'o-')
p.xlim([0.5,3.5])
p.ylim([-1,15])
p.show()

enter image description here

答案 1 :(得分:1)

您可以将矩阵重新整形为矢量,然后散点图:

# repeat the b vector for the amount of rows a has
x = np.repeat(b,a.shape[0])
# now reshape the a matrix to generate a vector
y = np.reshape(a.T,(1,np.product(a.shape) ))

# plot
import matplotlib.pyplot as plt
plt.scatter(x,y)
plt.show()

结果:

https://docs.angularjs.org/api/ng/function/angular.toJson