I'm trying to graph this function
=1/4cos**2(θA0 − θB0) + cos**2(θA0 − θB1)+cos**2(θA1 − θB0) + sin**2(θA1 − θB1)
this is my code so far:
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
import numpy as np
b = np.arange(0, 1, 0)
d = np.arange(0, 1, 0)
B, D = np.meshgrid(A0, B0)
nu = =1/4cos**2(θA0 − θB0) + cos**2(θA0 − θB1)+cos**2(θA1 − θB0) + sin**2(θA1 − θB1)
fig = plt.figure()
ax = Axes3D(fig)
ax.plot_surface(A0, B0, nu)
plt.xlabel('A0')
plt.ylabel('B0')
plt.show()
First, you need to import the math library to use the cosine and sine functions. So, you have to define all your constants and save the "nu" function value one at a time.
I assume A0 and B0 are constant, so you can represent your function in a 2D chart. In this case, the code is:
import matplotlib.pyplot as plt
import numpy as np
import math
# parameters
a0 = 1
a1 = 2
b0 = 3
b1 = 4
t = np.linspace(-(2 * np.pi), 2 * np.pi, 200) # theta
# formula
nu = []
for i in range(len(t)):
nu.append(1/4 * math.cos(t[i]*a0 - t[i]*b0)**2 + math.cos(t[i]*a0 - t[i]*b1)**2 +
math.cos(t[i]*a1 - t[i]*b0)**2 + math.sin(t[i]*a1 - t[i]*b1)**2)
plt.figure()
plt.plot(t, nu, color='red', marker='o')
plt.xlabel('a0')
plt.ylabel('b0')
plt.show()
Otherwise, you can modify A0 and B0 similar to theta and implement a 3D graph.
Related
How can i do this equation with library Matplotlib?
f(x) = exp(sqrt(x))/2 - sqrt(x**3)/5 + 2
import matplotlib as plt
import numpy as np
import math
fig, (ax1) = plt.subplots(nrows=2)
x = np.linspace(-3, 3, 100)
ax1.plot(x, 0.5.np.exp)
plt.show()
This represents your equation, but please double check it.
import math
e1 = lambda x: 0.5 * math.exp(-1 * math.sqrt(x)) - (0.2 * math.sqrt(x^3)) + 2
e1(1) # 1.901097008111102
I would like to make a 3D plot with several 2D line plot "slices" and shade the area between the x-axis and the curve (i.e. under the curve). When trying to do this with polygons I am getting filling but the correct areas are not being filled. Any help would be most appreciated!
%matplotlib notebook
import numpy as np
import matplotlib.pyplot as plt
from matplotlib.collections import PolyCollection
from mpl_toolkits.mplot3d import Axes3D
fig = plt.figure(figsize=(15,15))
ax = fig.add_subplot(111, projection='3d')
colors = ['r','b','g','m']
phi = [0,np.pi/4,np.pi/3, np.pi/2]
for c, k in zip(colors, phi):
eps2 = 0.001j
eps = np.linspace(-3,3,10000)
E = eps + eps2
gR = ((1-(((np.cos(k)+np.sin(k)*1j)**2)/((E+np.sqrt(1-E**2)*1j)**4)))/(1+(((np.cos(k)+np.sin(k)*1j)**2)/((E+np.sqrt(1-E**2)*1j)**4))))*1j
N = gR.imag
utol = 2
N[N>utol] = 2
ax.plot(eps, N, k,zdir='y', color=c)
verts = [list(zip(eps,N))]
poly = PolyCollection(verts, facecolors=c)
poly.set_alpha(1)
ax.add_collection3d(poly, zs=k,zdir='y')
ax.set_xlabel('Energy')
ax.set_ylabel('Phi')
ax.set_zlabel('DOS')
ax.set_yticks(phi)
ax.set_zlim(0,2)
ax.set_ylim(0,2)
plt.show()
Incorrect Plot for reference:
You created a polygon by connecting the first and last vertex of your curves. As these vertices have y = 2 everything gets connected with the horizontal line at that y-value.
To close the polygon at zero, repeat the first and the last x-value (np.pad(eps, 1, mode='edge')) and pad the y-values with a zero at both ends (np.pad(N, 1)).
If desired, ax.set_yticklabels(...) can show the y-ticks as a formula with pi.
Further, matplotlib seems to have a serious problem about deciding the relative depth of each polygon, showing them all mixed up. A workaround could be to rotate everything 180 degrees, e.g. by setting ax.view_init(elev=22, azim=130).
import numpy as np
import matplotlib.pyplot as plt
from matplotlib.collections import PolyCollection
from mpl_toolkits.mplot3d import Axes3D
fig = plt.figure(figsize=(15, 15))
ax = fig.add_subplot(111, projection='3d')
colors = ['r', 'b', 'g', 'm']
phi = [0, np.pi / 4, np.pi / 3, np.pi / 2]
for c, k in zip(colors, phi):
eps2 = 0.001j
eps = np.linspace(-3, 3, 10000)
E = eps + eps2
gR = ((1 - (((np.cos(k) + np.sin(k) * 1j) ** 2) / ((E + np.sqrt(1 - E ** 2) * 1j) ** 4))) / (
1 + (((np.cos(k) + np.sin(k) * 1j) ** 2) / ((E + np.sqrt(1 - E ** 2) * 1j) ** 4)))) * 1j
N = gR.imag
utol = 2
N[N > utol] = 2
ax.plot(eps, N, k, zdir='y', color=c)
verts = [list(zip(np.pad(eps, 1, mode='edge'), np.pad(N, 1)))]
poly = PolyCollection(verts, facecolors=c)
poly.set_alpha(1)
ax.add_collection3d(poly, zs=k, zdir='y')
ax.set_xlabel('Energy')
ax.set_ylabel('Phi')
ax.set_zlabel('DOS')
ax.set_yticks(phi)
ax.set_yticklabels(['$0$' if k == 0 else f'$\pi / {np.pi / k:.0f}$' for k in phi])
ax.set_zlim(0, 2)
ax.set_ylim(0, 2)
ax.view_init(elev=22, azim=130)
plt.show()
I am working on a piece of code which models the expansion of the universe, in particular I am integrating (using scipy odeint) the Friedmann equation to get a plot of the scale factor against time for different curvature. My code is:
import numpy as np
import matplotlib.pyplot as plt
import scipy as sp
from scipy.integrate import odeint
t_0 = 0.0004
a_0 = 0.001
omega_m = 1
omega_r = 0
omega_lambda = 0
omega_k = 1 - omega_lambda - omega_m - omega_r
H_0 = 1./13.799
def Friedmann(a, t):
dadt = H_0 * (((omega_m) * a**(-1)) + ((omega_r) * a**(-2)) + ((omega_lambda) * a**2) + (omega_k))**(1./2.)
return dadt
plt.rc('text', usetex=True)
plt.rc('font', family='serif')
fig, ax = plt.subplots(nrows=1,ncols=1)
for omega_k in np.linspace(-1, 1, 3):
t = np.linspace(t_0,50,200)
a = odeint(Friedmann, a_0, t)
a = np.array(a).flatten()
ax.plot(t,a)
line1, = plt.plot(t, a, 'b')
plt.xlabel('Time/Gyr')
plt.ylabel('Scale factor a')
plt.grid(True)
plt.axis([0,50,0,5])
plt.show()
This is the plot I get. I am trying to get a plot where the bottom (light blue) line loops back down and meets the x-axis at t ~ 43 Gyr but I can't seem to get this (maybe something to do with the integration).
Any help is much appreciated.
I've got a question. Below is my code snippet where I am trying to fill a vector given a function yv. When I run the code, there is no error, but it does not print out a result, nor does it show the plot I want.
import matplotlib as plt
import numpy as np
import math as m
e = 2.17
sigma = 1
mu = 0
xv = np.linspace(-4, 4, 100)
for rows in range(0):
for cols in range(100):
yv = 1 / (sigma * (2 * m.pi) ** (-0.5)) * e ** (-0.5) * ((((xv - mu) / sigma)) ** 2)
print('xv= {}'.format(xv))
print('yv= {}'.format(yv))
plt.plot(xv, yv, 'b-o', linewidth = 2, label = 'xv vs. yv')
plt.show()
What am I missing?
Thanks again!
Brandon
Your matplotlib import was not quite right. Try this:
from matplotlib import pyplot as plt
import numpy as np
import math as m
e = 2.17
sigma = 1
mu = 0
xv = np.linspace(-4, 4, 100)
yv = 1/(sigma*(2*m.pi)**(-0.5))*e**(-0.5)*((((xv-mu)/sigma))**2)
print('xv= {}'.format(xv))
print('yv= {}'.format(yv))
plt.plot(xv, yv, 'b-o', linewidth=2, label='xv vs. yv')
plt.show()
How to plot Ekman spiral curve in 2-D figure (u-v plot)
I have the following equation:
z = rang from (-228,0)
u = V0*cos(alpha*z + 3*pi/4)*e^(alpha*z)
v = V0*sin(alpha*z + 3*pi/4)*e^(alpha*z)
V0 = 0.1314; alpha = 0.013738, Az = 0.1, f = 3.775e-05
How could I use these equation to plot this figure?
from numpy import cos, sin, e, pi, linspace
import matplotlib.pyplot as plt
z = linspace(-228,0,1000)
V0 = 0.1314
alpha = 0.013738
u = V0*cos(alpha*z + 3*pi/4)*e**(alpha*z)
v = V0*sin(alpha*z + 3*pi/4)*e**(alpha*z)
plt.plot(u,v)
plt.show()