2

I have 2 separate data files with multiple entries like so:

File1.dat          File2.dat
x1 y1 z1           x2 y2 z2
x1 y1 z1           x2 y2 z2
...                ...
x1 y1 z1           x2 y2 z2

Is there a way to plot 3D vectors from the point in row 1 of File 1: (x1 y1 z1) to the point in row 1 of File 2: (x2 y2 z2) for all the entries? I guess this is called a quiver plot

4

You can plot a quiver plot with data from a single file doing

\documentclass[tikz]{standalone}
\usepackage{filecontents}
\usepackage{pgfplots}
\pgfplotsset{width=7cm,compat=1.16}

\begin{filecontents*}{data.txt}
x y z u v w
0 1 2 3 4 5 
1 2 3 4 5 0
2 3 4 5 0 1
\end{filecontents*}

\begin{document}
\begin{tikzpicture}
\begin{axis}[
]
\addplot3 [blue,-stealth,samples=10,quiver,quiver/.cd,u=\thisrow{u},v=\thisrow{v},w=\thisrow{w}] table {data.txt};
\end{axis}
\end{tikzpicture}
\end{document}

So I guess one way on doing this would be to gather the two files together prior plotting.

EDIT Here is a starting point to help lerge the two datasets into one table using pgfplotstable, building the table works but not the plotting. I have to figure why

\documentclass[]{article}
\usepackage{filecontents}
\usepackage{pgfplots}
\pgfplotsset{width=7cm,compat=1.16}
\usepackage{pgfplotstable}

\begin{filecontents*}{dataxyz.txt}
x y z
0 1 2 
1 2 3
2 3 4
\end{filecontents*}

\begin{filecontents*}{datauvw.txt}
u v w
3 4 5 
4 5 0
5 0 1
\end{filecontents*}

\begin{document}
% loads dataxyz.txt into \outputxyz
\pgfplotstableread{dataxyz.txt}{\outputxyz}
% transposes \outputxyz
\pgfplotstabletranspose[input colnames to={}]{\outputxyz}{\outputxyz}
% concatenates \outputxyz to \output
\pgfplotstablevertcat{\output}{\outputxyz}
% loads datauvw.txt into \outputuvw
\pgfplotstableread{datauvw.txt}{\outputuvw}
% transposes \outputuvw
\pgfplotstabletranspose[input colnames to={}]{\outputuvw}{\outputuvw}
% concatenates \outputuvw to \output
\pgfplotstablevertcat{\output}{\outputuvw}
% transposes output
\pgfplotstabletranspose[input colnames to={}]{\output}{\output}

% display output
%\pgfplotstabletypeset[string type]\outputxyz
%\pgfplotstabletypeset[string type]\outputuvw
%\pgfplotstabletypeset[string type]\output

% Working plot
\begin{tikzpicture}
\begin{axis}[]
\addplot3 [blue,-stealth,quiver,quiver/.cd,u=\thisrow{3},v=\thisrow{4},w=\thisrow{5}] table[] {\output};
\end{axis}
\end{tikzpicture}

\end{document}

EDIT 2 by Max
In my initial comment I pointed out that the plot works when using the input colnames to={} option for all transpose actions, but this way you lose the column names. If you use colnames from=colnames and input colnames to={} only for the last transpose, the column names are kept, and the plot can be made as if the two tables were in one file.

\documentclass[]{article}
\usepackage{filecontents}
\usepackage{pgfplots}
\pgfplotsset{width=7cm,compat=1.16}
\usepackage{pgfplotstable}

\begin{filecontents*}{dataxyz.txt}
x y z
0 1 2 
1 2 3
2 3 4
\end{filecontents*}

\begin{filecontents*}{datauvw.txt}
u v w
3 4 5 
4 5 0
5 0 1
\end{filecontents*}

\begin{document}

    % loads dataxyz.txt into \outputxyz
    \pgfplotstableread{dataxyz.txt}{\outputxyz}

    % transposes \outputxyz
    \pgfplotstabletranspose{\outputxyzT}{\outputxyz}

    % concatenates \outputxyz to \output
    \pgfplotstablevertcat{\outputT}{\outputxyzT}

    % loads datauvw.txt into \outputuvw
    \pgfplotstableread{datauvw.txt}{\outputuvw}

    % transposes \outputuvw
    \pgfplotstabletranspose{\outputuvwT}{\outputuvw}

    % concatenates \outputuvw to \output
    \pgfplotstablevertcat{\outputT}{\outputuvwT}

    % transposes output
    \pgfplotstabletranspose[colnames from=colnames,input colnames to={}]{\output}{\outputT}

    \begin{tabular}{rr}
           As read xyz: & \mbox{\pgfplotstabletypeset[string type]\outputxyz}  \\[1cm]
        Transposed xyz: & \mbox{\pgfplotstabletypeset[string type]\outputxyzT} \\[1cm]
           As read uvw: & \mbox{\pgfplotstabletypeset[string type]\outputuvw}  \\[1cm]
        Transposed uvw: & \mbox{\pgfplotstabletypeset[string type]\outputuvwT} \\[1cm]
         Concatenation: & \mbox{\pgfplotstabletypeset[string type]\outputT}    \\[1cm]
                 Final: & \mbox{\pgfplotstabletypeset[string type]\output}     \\[1cm]
    \end{tabular}



    % Working plot
    \begin{tikzpicture}
    \begin{axis}[xlabel=x,ylabel=y,grid=both]
    \addplot3 +[
        blue,
        -stealth,
    %    x=\thisrow{x},
    %    y=\thisrow{y},
    %    z=\thisrow{z},
        quiver,
        quiver/.cd,
            u=\thisrow{u},
            v=\thisrow{v},
            w=\thisrow{w}
        ] table {\output};
    \end{axis}
    \end{tikzpicture}

\end{document}

This results in

enter image description here

And the plot is

enter image description here

Edit 3 by Max
A small edit to show how to use this to draw from (x1,y1,z1) to (x2,y2,z2), with some very simple example coordinates:

enter image description here

The important changes are that the coordinates are named as in your question, and the expression for u, v, and w are changed to:

u={\thisrow{x2}-\thisrow{x1}},
v={\thisrow{y2}-\thisrow{y1}},
w={\thisrow{z2}-\thisrow{z1}},

Complete MWE:

\documentclass[]{article}
\usepackage{filecontents}
\usepackage{pgfplots}
\pgfplotsset{width=6cm,compat=1.16}
\usepackage{pgfplotstable}

\begin{filecontents*}{dataxyz.txt}
x1 y1 z1
1 0 0
0 1 0
0 0 1
\end{filecontents*}

\begin{filecontents*}{datauvw.txt}
x2 y2 z2
1 0 1
0 1 1
1 1 1
\end{filecontents*}

\begin{document}

    % loads dataxyz.txt into \outputxyz
    \pgfplotstableread{dataxyz.txt}{\outputxyz}

    % transposes \outputxyz
    \pgfplotstabletranspose{\outputxyzT}{\outputxyz}

    % concatenates \outputxyz to \output
    \pgfplotstablevertcat{\outputT}{\outputxyzT}

    % loads datauvw.txt into \outputuvw
    \pgfplotstableread{datauvw.txt}{\outputuvw}

    % transposes \outputuvw
    \pgfplotstabletranspose{\outputuvwT}{\outputuvw}

    % concatenates \outputuvw to \output
    \pgfplotstablevertcat{\outputT}{\outputuvwT}

    % transposes output
    \pgfplotstabletranspose[colnames from=colnames,input colnames to={}]{\output}{\outputT}

    % Working plot
    \begin{tikzpicture}

        \begin{axis}[xlabel=x,ylabel=y,zlabel=z,grid=both]
            \addplot3 +[
                blue,
                -stealth,
                x=\thisrow{x1},
                y=\thisrow{y1},
                z=\thisrow{z1},
                quiver,
                quiver/.cd,
                    u={\thisrow{x2}-\thisrow{x1}},
                    v={\thisrow{y2}-\thisrow{y1}},
                    w={\thisrow{z2}-\thisrow{z1}},
                ] table {\output};
        \end{axis}
    \end{tikzpicture}

\end{document}
  • 1
    If you change all \pgfplotstabletranspose to \pgfplotstabletranspose[input colnames to={}], and the \thisrownos to \thisrow, it should work. – Max Aug 18 '18 at 12:23
  • 1
    I edited your answer to include a way to keep the headers. If you do not agree, please feel free to rollback. – Max Aug 18 '18 at 14:10

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.