简体   繁体   中英

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. 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:

Scatter Plot

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:

数字

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM