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

iro*_*man 12 python plot vector matplotlib

在这里,我有一个参数方程.

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()
Run Code Online (Sandbox Code Playgroud)

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

![在此处输入图像说明

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

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

像这样的东西:

Python/matplotlib:绘制三维立方体,球体和矢量?

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

这样的事情可能吗?我只能找到矢量字段,什么不能.

像这样的东西. 在此输入图像描述

谢谢.

编辑问题

# 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)
Run Code Online (Sandbox Code Playgroud)

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

cri*_*007 5

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

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

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

我拿走了你所拥有的,Arrow3D这个答案中添加了类,并添加了一个简单的for循环来自一些样本值t.

#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)
Run Code Online (Sandbox Code Playgroud)
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)
Run Code Online (Sandbox Code Playgroud)

矢量