Mati Malik - 10 months ago 60

Python Question

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?

Answer

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: