2

I have a very large data file (.txt) organized as the following example:

0 1 1 . .

1 1 1 . . 

2 1 1 . .

. . . . .

I classically plotted a complex function f(Y,\thisrowno(0),\thisrowno(1),\thisrowno(2),...) using this data, this leads to many parallel curves. I want to connect vertically each node of a given curve with the corresponding node in the next curve. The resulting graph should look like a mesh. Is there any easy way to achieve that?

Here is a simple example:

\documentclass[10pt,a4paper]{article}
\usepackage[utf8]{inputenc}
\usepackage[]{geometry}
\usepackage{amsmath}
\usepackage{amsfonts}
\usepackage{amssymb}
\usepackage{graphicx}
\usepackage{pgfplots}
\usepackage{tikz} 
\begin{document}
\begin{figure}
\centering
\begin{tikzpicture}[scale=2]
\begin{axis}[legend entries={},
      xmin=-6, xmax=6,     ]                                 
     \foreach \i in {0,1,...,4}{     
     \addplot[thick]table[x expr=\thisrowno{0}, y expr=\i*\thisrowno{2}^2+\i]{test.txt};
     }      
\end{axis}
\end{tikzpicture}
\end{figure}
\end{document}

where test.txt contains the following:

-5 -5  5

-4 -4  4

-3 -3  3

-2 -2  2

-1 -1  1

 0  0  0 

 1  1  1

 2  2  2

 3  3  3 

 4  4  4

 5  5  5
  • Welcome to TeX.SX. It would be very nice if you could add a MWE which would make it much easier for us just modify the code and give an answer. – Stefan Pinnow Apr 20 '16 at 8:07
  • 1
    Do you really have empty lines in your txt-files? – sodd Apr 20 '16 at 17:38
1

So you mean something like the following?

% used PGFPlots v1.14
    % if you want that the points should be connected, don't have empty lines
    % in the data file
    \begin{filecontents*}{test.txt}
        -5 -5  5
        -4 -4  4
        -3 -3  3
        -2 -2  2
        -1 -1  1
         0  0  0
         1  1  1
         2  2  2
         3  3  3
         4  4  4
         5  5  5
     \end{filecontents*}
\documentclass[border=5pt]{standalone}
\usepackage{pgfplots}
    \pgfplotsset{
        compat=1.14,
    }
    % store the number of data rows in a variable (for later use)
    \pgfplotstablegetrowsof{test.txt}
        \pgfmathsetmacro{\NoOfRows}{\pgfplotsretval-1}
\begin{document}
\begin{tikzpicture}
    \begin{axis}[
        xmin=-6,
        xmax=6,
        % we don't want to show any text in the `nodes near coords'
        nodes near coords={},
        nodes near coords style={
            % actually we only need them as `coordinate's
            coordinate,
            % and name them according to the following scheme
            name=node-\plotnum-\coordindex,
        },
        % !!! now comes a critical point !!!
        % Option a)
        % to make that solution work you have set the following option due to
        % technical reasons which is roughly:
        % normally the markers are first collected and are drawn *after*
        % `\end{axis}', in contrast to the `\draw' command. Said that, the
        % named `nodes near coords' are not available during the execution of
        % the `\draw' command
        clip marker paths=true,
    ]
        % use a loop to draw the plots (as before)
        \foreach \i in {0,...,4} {
             \addplot+ [thick] table [
                % (in this case this is much simpler than using `x expr=\thisrowno{0}')
                x index=0,
                y expr=\i*\thisrowno{2}^2+\i,
             ] {test.txt};
         }
        % now use a loop to cycle through each `\addplot' command
        % because we want to connect each point with the same "x value" with
        % the one of the next `\addplot', we use the `remember' feature of the
        % `\foreach' command
        \foreach \i [remember=\i as \lasti (initially 0)] in {1,...,4} {
            % use another loop that cycles through each "x value"
            \foreach \j in {0,...,\NoOfRows} {
                \edef\temp{\noexpand%
                    \draw [green] (node-\lasti-\j) -- (node-\i-\j)
                        % these `node's are just drawn for debugging purposes
                        node [midway,inner sep=0pt,fill=green] {}
                    ;
                }\temp
            }
        }
    \end{axis}
\end{tikzpicture}
\end{document}

image showing the result of above code

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.