python 3d绘图 范围,如何在python中2D变量的3D绘图功能?

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, which varies between 0 and infinite, but I would like to visualize it from 0 to 3, in 0.2 intervals.

damping ratio, d, which varies between 0 and infinite, but I would like to plot it from 0 to 1, in 0.1 intervals.

finally, nu, which is a function that varies according to the two variables before.

7r3Hl.jpg?s=256

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.

解决方案

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)

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值