2014-11-25 3 views
0

아래의 코드는 다른 수직 레벨에서 벡터를 플롯 할 수있게하지만이 벡터에는 화살촉이 붙지 않습니다. 벡터의 끝에 화살촉을 얻을 수 있도록이 코드를 어떻게 수정해야하는지 궁금합니다.파이썬의 3d 벡터

#!/usr/bin/python 

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

fig = plt.figure() 
ax = fig.add_subplot(111, projection='3d') 
ax.plot([0,0.7], [0,0.5],zs=[1,1]) 
ax.plot([0,-0.3], [0,0.7],zs=[2,2]) 
ax.plot([0,-0.3],[0,0],zs=[3,3]) 

ax.set_xlim([0,3]) 
ax.set_ylim([3,0]) 
ax.set_zlim([0,4]) 
plt.show() 

답변

1

는, 당신은 FancyArrowPatch에서 상속 및 화살촉과 선 그리기에 대한 책임 클래스를 만들 수 있습니다 당신은 그들에게이 그들에 따르면 멋진 Putting arrowheads on vectors in matplotlib's 3d plot 의 예와 Python/matplotlib : plotting a 3d cube, a sphere and a vector?

에 있습니다.

전체 코드는 다음과 같을 수 있습니다 :

import matplotlib.pyplot as plt 
from mpl_toolkits.mplot3d import Axes3D 
from matplotlib.patches import FancyArrowPatch 
import numpy as np 
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) 


fig = plt.figure() 
ax = fig.add_subplot(111, projection='3d') 
# lines were replaced by Arrow3D below, so they might be no longer needed 
# ax.plot([0,0.7], [0,0.5],zs=[1,1]) 
# ax.plot([0,-0.3], [0,0.7],zs=[2,2]) 
# ax.plot([0,-0.3],[0,0],zs=[3,3]) 

ax.set_xlim([0, 3]) 
ax.set_ylim([3, 0]) 
ax.set_zlim([0, 4]) 

a = Arrow3D([0, 0.7], [0, 0.5], [1, 1], mutation_scale=20, lw=1, arrowstyle="->", color="b") 
b = Arrow3D([0, -0.3], [0, 0.7], [2, 2], mutation_scale=20, lw=1, arrowstyle="->", color="r") 
c = Arrow3D([0, -0.3], [0, 0], [3, 3], mutation_scale=20, lw=1, arrowstyle="->", color="g") 
ax.add_artist(a) 
ax.add_artist(b) 
ax.add_artist(c) 
plt.show() 

나는이 조금 도움이되기를 바랍니다. 더 많은 화살표 스타일을 보려면 matplotlib.patches documentation을 방문하십시오.