11

I would like to draw some 2D and 3D vectors in Tikz with certain properties. The following primitive graph sums what I desire:

g

  • I just need the first quadrant of the Cartesian coordinate system for my representation.

  • I need the vector to be connected to the respective values on the distinct dimensions(not a projection, just orthogonal dashed lines).

The code segment below does what I want to achieve in 3D with two exceptions:

  • The projections on the xy plane and z dimension are given instead of connecting the head of the vector to the corresponding value in each dimension.

  • Numerical values are missing in dimensions.

Code:

\documentclass[tikz]{standalone}
\usepackage{tikz-3dplot}
\begin{document}

\tdplotsetmaincoords{60}{120} \begin{tikzpicture} [scale=3, tdplot_main_coords, axis/.style={->,blue,thick}, vector/.style={-stealth,red,very thick}, vector guide/.style={dashed,red,thick}]

%standard tikz coordinate definition using x, y, z coords \coordinate (O) at (0,0,0);

%tikz-3dplot coordinate definition using r, theta, phi coords \tdplotsetcoord{P}{.8}{55}{60}

%draw axes \draw[axis] (0,0,0) -- (1,0,0) node[anchor=north east]{$x$}; \draw[axis] (0,0,0) -- (0,1,0) node[anchor=north west]{$y$}; \draw[axis] (0,0,0) -- (0,0,1) node[anchor=south]{$z$};

%draw a vector from O to P \draw[vector] (O) -- (P);

%draw guide lines to components \draw[vector guide] (O) -- (Pxy); \draw[vector guide] (Pxy) -- (P); \end{tikzpicture} \end{document}

alexT
  • 293

2 Answers2

13

I think the lines in black (or magenta) are the ones you are looking for:

enter image description here

To compute the Cartesian coordinates I use \pgfmathsetmacro.

Notes:

  • I don't know if one can easily extract the x, y, and z coordinates directly from setting of \tdplotsetcoord so I had to resort to defining them separately.

Code:

\documentclass[tikz]{standalone}
\usepackage{tikz-3dplot}
\begin{document}

\tdplotsetmaincoords{60}{120}

\newcommand{\Prho}{.8}% \newcommand{\Ptheta}{55}% \newcommand{\Pphi}{60}%

\begin{tikzpicture} [scale=3, tdplot_main_coords, axis/.style={->,blue,thick}, vector/.style={-stealth,red,very thick}, vector guide/.style={dashed,red,thick}]

%standard tikz coordinate definition using x, y, z coords \coordinate (O) at (0,0,0);

%tikz-3dplot coordinate definition using r, theta, phi coords \tdplotsetcoord{P}{\Prho}{\Ptheta}{\Pphi}

%draw axes \draw[axis] (0,0,0) -- (1,0,0) node[anchor=north east]{$x$}; \draw[axis] (0,0,0) -- (0,1,0) node[anchor=north west]{$y$}; \draw[axis] (0,0,0) -- (0,0,1) node[anchor=south]{$z$};

%draw a vector from O to P \draw[vector] (O) -- (P);

%draw guide lines to components \draw[vector guide] (O) -- (Pxy); \draw[vector guide] (Pxy) -- (P);

% Compute x,y,z \pgfmathsetmacro{\PxCoord}{\Prho * sin(\Pphi) * cos(\Ptheta)}% \pgfmathsetmacro{\PyCoord}{\Prho * sin(\Pphi) * sin(\Ptheta)}% \pgfmathsetmacro{\PzCoord}{\Prho * cos(\Pphi)}%

\draw[vector guide, black] (Pxy) -- (Px) node [left] {\PxCoord}; \draw[vector guide, black] (Pxy) -- (Py) node [above right] {\PyCoord};

\draw[vector guide, magenta] (P) -- (Pxz) node [left] {\PxCoord}; \draw[vector guide, magenta] (P) -- (Pyz) node [right] {\PyCoord};

\end{tikzpicture} \end{document}

Peter Grill
  • 223,288
  • 1
    is there a way to put label text to the red arrow and other component of the plot? – sunxd Nov 04 '19 at 08:17
  • 1
    @sunxd: Perhaps I am misunderstanding something, but the code in the question shows a few \nodes at the end of the arrows which can be used for label text. If that does not solve your issue, please post a new question with a MWE including \documentclass and the appropriate packages that sets up the problem. Also, link to this answer if it is relevant to the question. – Peter Grill Nov 04 '19 at 18:16
  • 1
    sorry, I am stupid, I did not look at the code carefully, yes, that works. – sunxd Nov 04 '19 at 18:58
  • 2
    @sunxd: No worries, I am sure I have made many such mistakes. – Peter Grill Nov 04 '19 at 22:24
13

This alternative provides Cartesian coordinates, serving as a complement to Perter Grill's solution.

enter image description here

Code

\documentclass[tikz,border=1cm]{standalone} 
\usepackage{tikz-3dplot} 
\begin{document}

\tdplotsetmaincoords{60}{120} 
\begin{tikzpicture} [scale=3, tdplot_main_coords, axis/.style={->,blue,thick}, 
vector/.style={-stealth,red,very thick}, 
vector guide/.style={dashed,red,thick}]

%standard tikz coordinate definition using x, y, z coords
\coordinate (O) at (0,0,0);

%tikz-3dplot coordinate definition using x, y, z coords

\pgfmathsetmacro{\ax}{0.8}
\pgfmathsetmacro{\ay}{0.8}
\pgfmathsetmacro{\az}{0.8}

\coordinate (P) at (\ax,\ay,\az);

%draw axes
\draw[axis] (0,0,0) -- (1,0,0) node[anchor=north east]{$x$};
\draw[axis] (0,0,0) -- (0,1,0) node[anchor=north west]{$y$};
\draw[axis] (0,0,0) -- (0,0,1) node[anchor=south]{$z$};

%draw a vector from O to P
\draw[vector] (O) -- (P);

%draw guide lines to components
\draw[vector guide]         (O) -- (\ax,\ay,0);
\draw[vector guide] (\ax,\ay,0) -- (P);
\draw[vector guide]         (P) -- (0,0,\az);
\draw[vector guide] (\ax,\ay,0) -- (0,\ay,0);
\draw[vector guide] (\ax,\ay,0) -- (0,\ay,0);
\draw[vector guide] (\ax,\ay,0) -- (\ax,0,0);
\node[tdplot_main_coords,anchor=east]
at (\ax,0,0){(\ax, 0, 0)};
\node[tdplot_main_coords,anchor=west]
at (0,\ay,0){(0, \ay, 0)};
\node[tdplot_main_coords,anchor=south]
at (0,0,\az){(0, 0, \az)};
\end{tikzpicture}
\end{document}
Jesse
  • 29,686
  • 1
    How would I be able to name the vector as well(like the dimensions)? Simply adding a letter (e.g. 'a') on top of the vector? – alexT Mar 07 '14 at 13:12
  • 1
    No worries, solved it! – alexT Mar 07 '14 at 13:14
  • 1
    OK, by the way, on the last 3 lines (\ax,0,0) for coordinate labels can be replaced by a single coordinate \ax as your primitive graph shows. – Jesse Mar 07 '14 at 13:27
  • 1
    @alexT, could you share how did you achieve that of having labeled text on the arrow and other element of the plot? – sunxd Nov 04 '19 at 08:18