如何在python中3D绘制2个变量的函数?
我正在尝试以 3D 方式绘制多种类型阻尼的振动放大系数.为了让那些不知道它是什么的人简化它,基本上,你有 3 个变量:
I am trying to 3D plot the magnification factor in vibrations for multiple types of damping. To simplify it for those who have no idea what it is, basically, you have 3 variables:
- beta,在 0 和无限之间变化,但我想以 0.2 的间隔将其可视化为从 0 到 3.
- 阻尼比 d 在 0 到无穷大之间变化,但我想以 0.1 的间隔从 0 到 1 绘制它.
- 最后是nu,这是一个根据之前的两个变量而变化的函数.
我的直觉说我应该用 (X,Y,Z) = (beta, d, nu) 来绘制它,但我刚刚开始使用这个库,我对 python 有点陌生,我只是使用它当我需要在课堂上可视化或计算问题时.我尝试为 beta 和 d 创建 2 个数组,但我不知道我应该为 nu 创建数组,因为它取决于两者.
My intuition says that I should plot this with (X,Y,Z) = (beta, d, nu), but I am just starting to use this library and I am kind of new to python, I just use it when I need to visualize or calculate problems in class. I tried creating 2 arrays for beta and d, but I don't know I should create the array for nu, since it depends on both.
这是我到现在为止的一段代码:
This is the piece of code I have until now:
import math
import matplotlib.pyplot as plt
import numpy as np
from mpl_toolkits.mplot3d import Axes3D
nu = []
b = [0.1 + i / 100 for i in range(0, 510)]
damp = [0.1 + i/10 for i in range(0,510)]
for d in damp:
nu_new = []
nu.append(nu_new)
for beta in b:
nu_new.append( math.sqrt(1+(2*d*beta)**2)/ math.sqrt((1-beta**2)**2+(2*d*beta)**2))
fig = plt.figure()
ax = Axes3D(fig)
ax.plot(b, d, nu)
plt.show()
我在试图绘制这个时有点卡住了,所以如果你有任何建议,我会很高兴.
I am kind of stuck trying to plot this, so if you have any suggestion I would be glad.
这应该有效:我不是 Python 专家,尤其是这两个 for 循环可能非常非 Pythonic,但它可以完成工作.
This should work: I'm not a Python expert and especially the two for loops might be very unpythonic, but it gets the job done.
import math
import matplotlib.pyplot as plt
import numpy as np
b = np.arange(0.2, 3.2, 0.2)
d = np.arange(0.1, 1.0, 0.1)
nu = np.zeros( (b.size, d.size) )
counter_y = 0
for deta in d:
counter_x = 0
for beta in b:
nu[counter_x, counter_y] = math.sqrt( 1 + (2*deta*beta)**2 ) / math.sqrt( (1-beta**2)**2 + (2*deta*beta)**2)
counter_x += 1
counter_y += 1
X, Y = np.meshgrid(d, b)
fig = plt.figure()
ax = fig.add_subplot(111, projection = '3d')
ax.plot_surface(X, Y, nu)