[英]How can I plot a graph of a function in Python?
I'm trying to graph this function我正在尝试绘制此 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.
因此,您必须定义所有常量并一次保存一个“nu” function 值。
I assume A0 and B0 are constant, so you can represent your function in a 2D chart.我假设 A0 和 B0 是恒定的,因此您可以在 2D 图表中表示您的 function。 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.否则,您可以修改 A0 和 B0 类似于 theta 并实现 3D 图。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.