0

I am a newbie in python and plotting stuff. I was trying to generate a plot using the following script. The goal was to draw a plot of Q vs F for all values.

from pylab import *

n = 5
D = 13
B = 10

x = linspace(-6.5, 6.5, 1000)
y = 1/sqrt(2*pi)*exp(-(x)**2/2)

for i in range(1,n):
    F = sum(y*cos(2*pi*i*x/D)*exp(-i**2*B/(4*D**2)))
    print F

for j in range(1,n):
    Q = 2*pi*(j)/D
    print Q

plt.plot(Q,F,'rx')
plt.show()

When I am running the script, it plots only one data point instead of all. I am sure, I did some stupid mistake. Someone could pls help me out here? Thank you.

2 Answers 2

2

It's because you're setting F and Q equal to the value on each loop, rather than appending the value to the end of an array.

from pylab import *

n = 5
D = 13
B = 10

x = linspace(-6.5, 6.5, 1000)
y = 1/sqrt(2*pi)*exp(-(x)**2/2)

F,Q = [],[]

for i in range(1,n):
    F.append(sum(y*cos(2*pi*i*x/D)*exp(-i**2*B/(4*D**2))))
    Q.append(2*pi*(j)/D)

plt.plot(Q,F,'rx')
plt.show()
Sign up to request clarification or add additional context in comments.

Comments

1

You are only setting Q, F equal in the loops.

from pylab import *

n = 5
D = 13
B = 10

x = linspace(-6.5, 6.5, 1000)
y = 1/sqrt(2*pi)*exp(-(x)**2/2)

for i in range(1,n):
    F.append(sum(y*cos(2*pi*i*x/D)*exp(-i**2*B/(4*D**2))))
    print F
    Q.append(2*pi*(j)/D)
    print Q

plt.plot(Q,F, 'rx')    
plt.show()

3 Comments

I'm not familiar with pylab, I didn't know it plotted lists.
It's just standard matplotlib.
Thank you for your help. Now I know what went wrong and learn about it.

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.