0

I have two lists, x and y, each 2000 elements long. They are plotted together in a scatterplot. I want to join the two lists together into a new list, pairing each x[i] with each y[i] so that I can print out a list that looks like (x[i], y[i]). Then, I want to randomly draw n samples from the new list, with replacement, and plot those samples on the same graph as the scatterplot. Here is what I have so far.

N = 2000
n = 35

x = (np.random.randn(N)/N)
y = x + sigma*(np.random.randn(N))

z = np.random.choice(len(x), size=n, replace=True)
pairs = [(x[i], y[i]) for i in z]

print(z)

plt.plot(x,y,'.')
plt.grid()

Am I on the right track or is there a better way to do this?

After some searching I found a new method that seems to work, but it makes the graph look really strange.

N = 2000
n = 35

x = (np.random.randn(N)/N)
y = x + (np.random.randn(N))
z = [[x, y] for x, y in zip(x, y)]

p = (random.choices(z, k=n))
print(p)

plt.plot(x,y,'.')
plt.plot(p,'.')
plt.grid()

All the dots from the x,y plot are pushed to the side while the dots from the p plot look more like a regular graph. Except that there is also a straight line of dots across the bottom of the graph. I have no idea what the heck is going on. Why is the p graph not plotted in the same area as the x,y plots? What is that straight line of dots across the bottom about?

  • 1
    Does this answer your question? [How to merge lists into a list of tuples?](https://stackoverflow.com/questions/2407398/how-to-merge-lists-into-a-list-of-tuples) – azro Mar 21 '21 at 16:28
  • Good but `zip` can do the pairing for you – azro Mar 21 '21 at 16:28
  • I found a way to do a zip function, but the graph looks really strange now. See my edit above. –  Mar 21 '21 at 16:44
  • You want to plot a sample or all points ? Why keeping `plt.plot(x,y,'.')` ? – azro Mar 21 '21 at 16:48
  • I want the x,y graph and the p graph plotted together. –  Mar 21 '21 at 16:50
  • So ALL POINTS + SAMPLE POINTS (from all points) ? – azro Mar 21 '21 at 16:50
  • Yes, that's what I'm looking for. Unfortunately, all the x,y dots are pushed to one side, resembling a vertical line while the p dots are more spaced apart. But for each p dot, there is a corresponding dot along the x-axis, making a straight line of dots. –  Mar 21 '21 at 16:56

1 Answers1

0

You may use zip twice

  • to pair x and y to be able to use random.choices (i'd suggest random.sample to get uniques)
  • to pair in the other way all values to get a sample_x and sample_y

And use plt.scatter


N = 2000
n = 35
x = np.random.randn(N) / N
y = x + np.random.randn(N)

# plot all
plt.scatter(x, y, marker='.')

# generate and plot sample
z = list(zip(x, y))
p = random.choices(z, k=100)
sample_x, sample_y = zip(*p)
plt.scatter(sample_x, sample_y, marker='+')

plt.grid()
plt.show()
azro
  • 53,056
  • 7
  • 34
  • 70