Reputation: 191
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.
Upvotes: 0
Views: 184
Reputation: 6526
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()
Upvotes: 0
Reputation: 339280
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()
Upvotes: 1
Reputation: 2346
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()
Upvotes: 0