Make scatter plot from set of points in tuples

2020-07-03 04:59发布

问题:

I have set of points in tuples, like this:

>>> s
set([(209, 147),
     (220, 177),
     (222, 181),
     (225, 185),
     (288, 173),
     (211, 155),
     (222, 182)])

What is the right way to do scatter plot of this set?

回答1:

You can do:

x,y = zip(*s)
plt.scatter(x, y)

Or even in an "one-liner":

plt.scatter(*zip(*s))

zip() can be used to pack and unpack arrays and when you call using method(*list_or_tuple), each element in the list or tuple is passed as an argument.



回答2:

x = []; y=[]
for point in s:
   x.append(point[0])
   y.append(point[1])
plt.scatter(x,y)


回答3:

If you want to use NumPy arrays you can use:

data = np.array(list(s))

First transform s to a list and than to a NumPy array.

Now you have a list op points, to get a lists of x's and y's you can use:

xs = data.transpose()[0]   # or xs = data.T[0] or  xs = data[:,0]
ys = data.transpose()[1]

And make a plot with:

plt.plot(xs, ys, 'ro')