[英]Matrix elements for scatter plot
I have this array: 我有这个数组:
b=np.array([1,2,3])
And this matrix: 和这个矩阵:
a=np.array([[ 4, 2, 12],
[ 7, 12, 0],
[ 10, 7, 10]])
I now want to create a scatter plot, which takes b[i] as the x-axis and a[j][i] as y-axis. 我现在想创建一个散点图,将b [i]作为x轴,将a [j] [i]作为y轴。 More specific i want the points/coordinates in my plot to be:
更具体地说,我希望绘图中的点/坐标为:
(b[i],a[j][i])
Which in my case will be: 在我的情况下将是:
(1,4) (1,7) (1,10) (2,2) (2,12) (2,7) (3,12) (3,0) (3,10)
Which i then easily can plot. 然后,我可以轻松地绘制出哪个。 The plot would look something like this:
情节看起来像这样:
Can anyone help me create the points for my plot? 谁能帮我为我的情节创建点? Is there a general solution?
有没有一般的解决方案?
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()
You can reshape the matrices to a vector and then scatter plot them: 您可以将矩阵重塑为矢量,然后将它们散布成图:
# 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()
Results in: 结果是:
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.