2016-04-23 56 views
1

我有這樣的陣列:爲散點圖矩陣元素

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

誰能幫助我創造我的情節點?有一個通用的解決方案嗎?

回答

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

結果:

figure

+0

謝謝!有一點修正tho。行a的數量是np.repeat(b,a.shape [0]),因爲a.shape [0]給出了行,而a.shape [1]給出了這些列。 –

+0

謝謝你的評論,你是對的, 修復。 – agold

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

+0

這是一個非常冷靜和簡單的方法來做到這一點。其實你可以製作一個循環:對於我在範圍內(len(b): plt.scatter(b,a [i]) plt.show() –