0

I am writting a very simple script, one that plot a sin using jupyter notebook (python 3). when I put:

import numpy
import matplotlib.pyplot as plt
x=np.arange(0.0,5*np.pi,0.001)
y = np.sin(x)
plt.plot(x,y)

The plot is fine.

However if :

import numpy
import matplotlib.pyplot as plt
x=np.arange(0.0,5*np.pi,0.001)
np.random.shuffle(x)
y = np.sin(x)
plt.plot(x,y)

the image is enter image description here

I don't understand why shuffling the x BEFORE I ran sin does it.
thank you

1 Answer 1

2

Let's first simplify things a bit. We plot 4 points and annote them with the order in which they are plotted.

import numpy as np; np.random.seed(42)
import matplotlib.pyplot as plt

x=np.arange(4)
y = np.sin(x)

plt.plot(x,y, marker="o")

for i, (xi,yi) in enumerate(zip(x,y)):
    plt.annotate(str(i), xy=(xi,yi), xytext=(0,4), 
                 textcoords="offset points", ha="center")

plt.show()

enter image description here

No if we shuffle x and plot the same graph,

x=np.arange(4)
np.random.shuffle(x)
y = np.sin(x)

enter image description here

we see that positions of the points are still are the same, but while e.g. previously the first point was the one at (0,0), it's now the third one appearing there. Due to this randomized order, the connecting lines go zickzack.

Now if you use enough points, all those lines will add up to look like a complete surface, which is what you get in your image.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.