PythonMPL:如何在3D向量图上绘制点和箭头的头部

PythonMPL:如何在3D向量图上绘制点和箭头的头部,python,numpy,matplotlib,vector,point,Python,Numpy,Matplotlib,Vector,Point,我有以下代码来生成一个三角形(从一些线)和一个3D图形中的向量: from mpl_toolkits.mplot3d import axes3d import matplotlib.pyplot as plt import numpy as np import sys # D is the viewing direction. # P is the "eye" point. # A, B, and C are the points of the Triangle being tested a

我有以下代码来生成一个三角形(从一些线)和一个3D图形中的向量:

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

import sys

# D is the viewing direction.
# P is the "eye" point.
# A, B, and C are the points of the Triangle being tested against.

# make a shear matrix from the D vector.
def make_shear(d):
    print [1, 0, -d[0]/d[2] ];
    print [1, 0, -d[1]/d[2]];
    print [0, 0, 1/d[2]] ;
    return np.array([ [1, 0, -d[0]*1.0/d[2] ], [0, 1, -d[1]*1.0/d[2]], [0, 0, 1.0/d[2]] ]);

def draw_line(ax, A, B, c):
    ax.plot([A[0], B[0]], [A[1],B[1]],[A[2],B[2]], color=c)

plt.close('all');

fig1 = plt.figure();

ax = fig1.add_subplot(111, projection='3d')

P = np.array([3, 4, 2]);
P1 = np.array([1, 5, 7]);
D = P1 - P;
M = make_shear(D);

print "D = ", D
#print D[0];

print "M = ";
print M;

Dp = M.dot(D);

A =np.array([-5, 3, 5]);
B =np.array([1, 4, 10]);
C =np.array([-5, 5, 5]);

Ap = M.dot(A-P);
Bp = M.dot(B-P);
Cp = M.dot(C-P);

U = np.dot(Dp, np.cross(Cp, Bp));
V = np.dot(Dp, np.cross(Ap, Cp));
W = np.dot(Dp, np.cross(Bp, Ap));
print "U = ", U;
print "V = ", V;
print "W = ", W;

ax = fig1.add_subplot(111, projection='3d')
ax.set_xlabel('X axis')
ax.set_ylabel('Y axis')
ax.set_zlabel('Z axis')

draw_line(ax, A, B, 'g');
draw_line(ax, B, C, 'g');
draw_line(ax, C, A, 'g');
sf = 5; # scale factor, to make the direction more obvious...not sure this works.
ax.quiver(P[0], P[1], P[2], sf*D[0], sf*D[1], sf*D[2]); # head_width=0.05, head_length=0.1, fc='k', ec=ki");

b = 10;
ax.set_xlim([-b, b]);
ax.set_ylim([-b, b]);
ax.set_zlim([-b, b]);
plt.show();
  • 我想在图上加一个点,比如P,作为一个红色的小球
  • 我想在quiver语句中绘制的向量的头部添加一个箭头
  • 做这两件事最直接的方法是什么

  • x.quiver…
    行之后添加以下行

    ax.scatter(P[0], P[1], P[2], c='r')
    
    v = [0, 1, 0]
    end_v = P + v
    ax.quiver(end_v[0], end_v[1], end_v[2], v[0], v[1], v[2])
    
  • x.quiver…
    行之后添加以下行

    ax.scatter(P[0], P[1], P[2], c='r')
    
    v = [0, 1, 0]
    end_v = P + v
    ax.quiver(end_v[0], end_v[1], end_v[2], v[0], v[1], v[2])