Mati Malik Mati Malik - 1 year ago 106
Python Question

Matrix elements for scatter plot

I have this array:


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:


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?

Answer Source

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

Results in:


Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download