TheCodeNovice
TheCodeNovice

Reputation: 688

Matplotlib have arrow point upwards in 3d space

I am trying to have an arrow point in the Z axis in a 3d subplot. I am able to get planar directions with no problem but having something orthoganal to the plane is proving difficult.

my code thus far

from mpl_toolkits.mplot3d import axes3d
import mpl_toolkits.mplot3d.art3d as art3d
from matplotlib.patches import Arrow, PathPatch
import matplotlib.pyplot as pl
import numpy as np


fig = pl.figure()
ax= fig.add_subplot(1, 1, 1, projection='3d')

arrowfig = Arrow(0,0,1,0,width =0.5, color = 'red')
ax.add_patch(arrowfig)
art3d.pathpatch_2d_to_3d(arrowfig, z=0, zdir="z")
arrowfig = Arrow(0,0,0,1,width =0.5, color = 'blue')
ax.add_patch(arrowfig)
art3d.pathpatch_2d_to_3d(arrowfig, z=0, zdir="z")
arrowfig = Arrow(0,0,0,-1,width =0.5, color = 'purple')
ax.add_patch(arrowfig)
art3d.pathpatch_2d_to_3d(arrowfig, z=0, zdir="z")
arrowfig = Arrow(0,0,-1,0,width =0.5, color = 'green')
ax.add_patch(arrowfig)
art3d.pathpatch_2d_to_3d(arrowfig, z=0, zdir="z")

ax.set_xlim([0,1])
ax.set_ylim([0,1])
ax.set_zlim([0,1])

pl.show()

Upvotes: 1

Views: 190

Answers (1)

spfrnd
spfrnd

Reputation: 936

To have a vector pointing out of the x-y-plane set the zdir argument of the pathpatch_2d_to_3d function to "x" or "y". It defines the axis that is normal to the plane that the transformed arrow will lie in.

art3d.pathpatch_2d_to_3d(arrowfig, z=0, zdir="x")

Upvotes: 1

Related Questions