为散点图矩阵元素

问题描述:

我有这样的阵列:为散点图矩阵元素

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

而这种矩阵:

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

我现在想要创建一个散点图,这需要B [I]作为x轴轴和a [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

谁能帮助我创造我的情节点?有一个通用的解决方案吗?

可以重塑矩阵到向量,然后散点图他们:

# 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() 

结果:

figure

+0

谢谢!有一点修正tho。行a的数量是np.repeat(b,a.shape [0]),因为a.shape [0]给出了行,而a.shape [1]给出了这些列。 –

+0

谢谢你的评论,你是对的, 修复。 – agold

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

+0

这是一个非常冷静和简单的方法来做到这一点。其实你可以制作一个循环:对于我在范围内(len(b): plt.scatter(b,a [i]) plt.show() –