如何在python中2D变量的3D绘图功能? [英] How to 3D plot function of 2 variables in python?
问题描述
我正在尝试对多种阻尼类型的振动进行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到3。 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循环可能非常不完整
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)
这篇关于如何在python中2D变量的3D绘图功能?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!