好吧,我有一个基本的脚本来绘制一个物体的轨迹。我有基本的运动方程,解决了物体相对于时间的位置。该图本身是对象轨迹的3D表示。
我已经成功设置了轴限制,现在我想确保没有看到任何超出轴限制的轨迹值。现在,轨迹落在x-y平面之下并继续向下,在3D绘图之外...有没有办法阻止这种情况?
以下是整个代码:
import matplotlib as mpl
from mpl_toolkits.mplot3d import Axes3D
import numpy as np
import matplotlib.pyplot as plt
mpl.rcParams['legend.fontsize'] = 10
fig = plt.figure()
ax = fig.gca(projection='3d')
### Define Variables ###
time = (10) #Time to calculate the equations
t = np.linspace(0, time, 100)
g = (9.81) #Gravity
vxo = (3) #initial velocity in the x-direction
vyo = (1) #initial velocity in the y-direction
vzo = (0) #initial velocity in the z-direction
xo = (0) #initial x-position
yo = (0) #initial y-position
zo = (9) #initial z-position
### Equations of Motion ###
x = (xo + (vxo * t))
y = (yo + (vyo * t))
z = (10 - (.5 * g * (t**2)))
ax.plot(x, y, z, label=('Trajectory of Soccer Ball ' + str(time)))
ax.legend()
### Set axis limits ###
ax.set_xlim3d(0,10)
ax.set_ylim3d(0,10)
ax.set_zlim3d(0,10)
plt.show()
答案 0 :(得分:0)
如果你的物体超出轴的极限;这是因为方程式告诉它这样做。在绘制结果之前,您必须约束结果并进行过滤。类似的东西:
x = equation1()
if( x > someLimit )
handle error / object bounces by inverting the direction perhaps
plot(x)