簡體   English   中英

散點圖的矩陣元素

[英]Matrix elements for scatter plot

我有這個數組:

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)

然后,我可以輕松地繪制出哪個。 情節看起來像這樣:

散點圖

誰能幫我為我的情節創建點? 有沒有一般的解決方案?

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

在此處輸入圖片說明

您可以將矩陣重塑為矢量,然后將它們散布成圖:

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

結果是:

數字

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM