0

I'm running a simple bit of code to produce a plot similar to this. However the plot I am getting from my code is empty - I assume this is a fairly simple error in my code as I'm new to this. My code is:

import matplotlib.pyplot as plt
import numpy as np

omega_0 = 0.6911
w_0 = -0.77
a = []

for a in range(0,1,100):
omega_phi = (omega_0*(a**(-3.0*w_0)))/((omega_0*(a**(-3.0*w_0))) +    1 - omega_0)
w = w_0 + (((w_0*(1.0 - w_0**2.0))/(1.0 - 2.0*w_0 + 4.0*w_0**2))*((omega_phi/(1.0 - omega_phi)))) + ((((-1.0)*w_0*(1.0 - w_0**2.0))/(1.0 - 3.0*w_0 + 12.0*w_0**2))*((omega_phi/(1.0 - omega_phi))**2.0)) + ((((1.0)*w_0*(1.0 - w_0**2.0))/(1.0 - 4.0*w_0 + 24.0*w_0**2))*((omega_phi/(1.0 - omega_phi))**3.0))
plt.plot(a,w)

plt.xlabel('a')
plt.ylabel('w')
plt.grid(True)
plt.show()

Any help is greatly appreciated.

1
  • 1
    You are not updating a anywhere and w is getting overwritten each iteration. Commented Mar 15, 2018 at 15:28

3 Answers 3

1

range(0,1,100) gives you a single point. But you cannot draw a line through a single point. Possibly you want range(0,100,1), instead, which gives you 100 points between 0 and 99, or np.linspace(0,1,100), which gives you 100 points between 0 and 1.

Next you better use numpy to calculate your values.

Finally, consider simplifying your equations a bit, such that they become readable.

import matplotlib.pyplot as plt
import numpy as np

omega_0 = 0.6911
w_0 = -0.77
a = np.arange(0,100,1) # or np.linspace(0,1,100) depending on what you want.
om = omega_0*(a**(-3.0*w_0))
omega_phi = om/(om + 1 - omega_0)
p = w_0*(1.0 - w_0**2.0)
q = omega_phi/(1.0 - omega_phi)
w = w_0 +   p/(1.0 - 2.0*w_0 +  4.0*w_0**2)*q      - \
            p/(1.0 - 3.0*w_0 + 12.0*w_0**2)*q**2.0 + \
            p/(1.0 - 4.0*w_0 + 24.0*w_0**2)*q**3.0

plt.plot(a,w)

plt.xlabel('a')
plt.ylabel('w')
plt.grid(True)
plt.show()

enter image description here

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

Comments

0

I suggest you the following update of your original code. I assumed you wanted to display the curve with a going from 0 to 1 by 0.01 step.
I also added some comments for readability and for you to better understand the code:

import matplotlib.pyplot as plt
import numpy as np

# Define constants
omega_0 = 0.6911
w_0 = -0.77

# Set 'a' values from 0 to 1 by 0.01 step
a = np.arange(0, 1, 0.01)

# Set 'w' values associated with each 'a' value
omega_phi = (omega_0*(a**(-3.0*w_0)))/((omega_0*(a**(-3.0*w_0))) +    1 - omega_0)
w = w_0 + (((w_0*(1.0 - w_0**2.0))/(1.0 - 2.0*w_0 + 4.0*w_0**2))*((omega_phi/(1.0 - omega_phi)))) + ((((-1.0)*w_0*(1.0 - w_0**2.0))/(1.0 - 3.0*w_0 + 12.0*w_0**2))*((omega_phi/(1.0 - omega_phi))**2.0)) + ((((1.0)*w_0*(1.0 - w_0**2.0))/(1.0 - 4.0*w_0 + 24.0*w_0**2))*((omega_phi/(1.0 - omega_phi))**3.0))

# Plot the curve with all (a,w) points
plt.plot(a,w)
plt.xlabel('a')
plt.ylabel('w')
plt.grid(True)

# Show the curve
plt.show()

4 Comments

if you're using numpy arrays, you don't need to iterate
@PaulH You're right, sir ! Thanks a lot, I have updated my post with this simplification.
@LaurentH. thanks for the reply, this gives me a similar plot to the one I'm trying to get but it isn't quite the same - I think maybe I'm trying to solve the w equation in the wrong way
@Brad You're welcome. You should accept the answer (mine or another) if it solves the question you asked in your original post.
0

There are several things going on. Firstly, you should not be calling plot in your loop. You call it once, where the arguments are your data points, in this case a and w. These should be lists.

Secondly, your range parameters were wrong; they should be `range(start, stop, step). In your code, you were going from 0 to 1 in with a stepsize of 1000, which would immediately go to 1. (I assume you wanted 1000 points from 0 to 1 instead? I've used simpler parameters.)

This should give you the desired plot.

import matplotlib.pyplot as plt

omega_0 = 0.6911
w_0 = -0.77
a = []
w = []

for i in range(0, 100, 1):
    omega_phi = (omega_0*(i**(-3.0*w_0)))/((omega_0*(i**(-3.0*w_0))) + 1 - omega_0)
    w.append(w_0 + (((w_0*(1.0 - w_0**2.0))/(1.0 - 2.0*w_0 + 4.0*w_0**2))*((omega_phi/(1.0 - omega_phi)))) + ((((-1.0)*w_0*(1.0 - w_0**2.0))/(1.0 - 3.0*w_0 + 12.0*w_0**2))*((omega_phi/(1.0 - omega_phi))**2.0)) + ((((1.0)*w_0*(1.0 - w_0**2.0))/(1.0 - 4.0*w_0 + 24.0*w_0**2))*((omega_phi/(1.0 - omega_phi))**3.0)))
    a.append(i)

print w, a
plt.plot(a, w)
plt.xlabel('a')
plt.ylabel('w')
plt.grid(True)
plt.show()

1 Comment

thanks for the reply, this doesn't really give me the plot I'm looking for (maybe I've been doing the maths wrong) but if I import numpy and just change the code to for i in np.linspace(0,1,100) I get a more similar plot to the one I'm trying to get

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.