Python - 在某些点绘制速度和加速度矢量

时间:2016-01-26 17:39:05

标签: python vector matplotlib plot

这里,我有一个参数方程。

import matplotlib.pyplot as plt
import numpy as np
from mpl_toolkits.mplot3d import Axes3D

t = np.linspace(0,2*np.pi, 40)

# Position Equation
def rx(t):
    return t * np.cos(t)
def ry(t):
    return t * np.sin(t)

# Velocity Vectors
def vx(t):
    return np.cos(t) - t*np.sin(t)
def vy(t):
    return np.sin(t) + t*np.cos(t)

# Acceleration Vectors
def ax(t):
    return -2*np.sin(t) - t*np.cos(t)

def ay(t):
    return 2*np.cos(t) - t*np.sin(t)

fig = plt.figure()
ax1 = fig.gca(projection='3d')

z = t 
ax1.plot(rx(z), r(z), z)
plt.xlim(-2*np.pi,2*np.pi)
plt.ylim(-6,6)
ax.legend()

所以我有这个参数方程来创建这个图。

![enter image description here

我在我的代码中定义了上面的速度和加速度参数方程。

我想要做的是在定义的点上绘制上面位置图中的加速度和速度矢量。 (Id est,t = pi / 2,3pi / 2,2pi)

这样的事情:

Python/matplotlib : plotting a 3d cube, a sphere and a vector?

但我想更直接地做一些事情,因为我必须将每个点t定义为两个方程式。

这样的事情可能吗?我只能找到矢量字段而不是。

像这样的东西。 enter image description here

谢谢。

修改问题

# t = pi/4 

t_val_start_pi4 = np.pi/4
vel_start_pi4 = [rx(t_val_start_pi4), ry(t_val_start_pi4), t_val_start_pi4]

vel_end_pi4 = [rx(t_val_start_pi4 ) + vx(t_val_start_pi4 ), ry(t_val_start_pi4 )+vy(t_val_start_pi4 ), t_val_start_pi4 ]

vel_vecs_pi4 = (t_val_start_pi4 , vel_end_pi4)

vel_arrow_pi4 = Arrow3D(vel_vecs_pi4[0],vel_vecs_pi4[1], vel_vecs_pi4[2], mutation_scale=20, lw=1, arrowstyle="-|>", color="b")

axes.add_artist(vel_arrow_pi4)

它会给我一个错误Tuple out of index

1 个答案:

答案 0 :(得分:5)

我觉得这很接近......甚至得到了与样本图片相匹配的颜色:)

我对极坐标的绘图经验不太熟悉(主要是在第三维t坐标上混淆)。

希望这会有所帮助,你可以弄清楚如何扩展它

我拿走了您的内容,添加了来自this answerArrow3D类,并在t的一些示例值上添加了一个简单的for循环。

#draw a vector
from matplotlib.patches import FancyArrowPatch
from mpl_toolkits.mplot3d import proj3d

class Arrow3D(FancyArrowPatch):
    def __init__(self, xs, ys, zs, *args, **kwargs):
        FancyArrowPatch.__init__(self, (0,0), (0,0), *args, **kwargs)
        self._verts3d = xs, ys, zs

    def draw(self, renderer):
        xs3d, ys3d, zs3d = self._verts3d
        xs, ys, zs = proj3d.proj_transform(xs3d, ys3d, zs3d, renderer.M)
        self.set_positions((xs[0],ys[0]),(xs[1],ys[1]))
        FancyArrowPatch.draw(self, renderer)
axes = fig.gca(projection='3d')

t_step = 8
for t_pos in range(0, len(t)-1, t_step):
    t_val_start = t[t_pos]
#     t_val_end = t[t_pos+1]

    vel_start = [rx(t_val_start), ry(t_val_start), t_val_start]
    vel_end = [rx(t_val_start)+vx(t_val_start), ry(t_val_start)+vy(t_val_start), t_val_start]
    vel_vecs = list(zip(vel_start, vel_end))
    vel_arrow = Arrow3D(vel_vecs[0],vel_vecs[1],vel_vecs[2], mutation_scale=20, lw=1, arrowstyle="-|>", color="g")
    axes.add_artist(vel_arrow)

    acc_start = [rx(t_val_start), ry(t_val_start), t_val_start]
    acc_end = [rx(t_val_start)+ax(t_val_start), ry(t_val_start)+ay(t_val_start), t_val_start]
    acc_vecs = list(zip(acc_start, acc_end))
    acc_arrow = Arrow3D(acc_vecs[0],acc_vecs[1],acc_vecs[2], mutation_scale=20, lw=1, arrowstyle="-|>", color="m")
    axes.add_artist(acc_arrow)

axes.plot(rx(t), ry(t), t)
plt.xlim(-2*np.pi,2*np.pi)
plt.ylim(-6,6)

vectors