HuShu
HuShu

Reputation: 501

Python: Scatter plot of data stored in matrix form

I have a row vector showing x coordinates

x=[1,5,7,3]

and I have a matrix

y=[[1,2,3,4],[2,3,1,4],[1,2,2,1],[2,3,1,1]]

whose rows represent the scatter of a variable at the corresponding x coordinate.

I want to make a scatter plot of this, i.e. for each x, there are 4 different y values, which I want to plot. Is there a way?

Upvotes: 1

Views: 521

Answers (3)

Sede
Sede

Reputation: 61225

You can do this using itertools.cycle and zip

from itertools import cycle

import matplotlib.pyplot as plt

x = [1,5,7,3]
y = [[1,2,3,4], [2,3,1,4], [1,2,2,1], [2,3,1,1]]
for i in zip(x, y):
    b = zip(*(zip(cycle([i[0]]), i[1])))
    plt.scatter(*b)

plt.show()

Then your plot looks like this: enter image description here

Upvotes: 0

HuShu
HuShu

Reputation: 501

Thanks @User3100115

Fidgeting around I also found another solution, maybe not as efficient as @User3100115

x=[1,5,7,3]
y=[[1,2,3,4],[2,3,1,4],[1,2,2,1],[2,3,1,1]]

er=np.ones(4)
k=0
while k<4:
     e=x[k]*er
     plt.scatter(e,y[k])
     plt.draw()
     k+=1
plt.show()  

Upvotes: 1

Sait
Sait

Reputation: 19815

import matplotlib.pyplot as plt

x=[1,5,7,3]
y=[[1,2,3,4],[2,3,1,4],[1,2,2,1],[2,3,1,1]]

for yy in y:
    plt.scatter(x,yy)

plt.show()

Upvotes: 0

Related Questions