使用 PGFPlots 绘制数据文件

使用 PGFPlots 绘制数据文件

我有一个非常大的数据文件(.txt),其组织方式如下例所示:

0 1 1 . .

1 1 1 . . 

2 1 1 . .

. . . . .

我使用这些数据绘制了一个经典的复杂函数f(Y,\thisrowno(0),\thisrowno(1),\thisrowno(2),...),这会产生许多平行曲线。我想将给定曲线的每个节点与下一条曲线中的相应节点垂直连接起来。生成的图形应该看起来像一个网格。有什么简单的方法可以实现这一点吗?

这是一个简单的例子:

\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}

在哪里测试.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

答案1

所以您的意思是下面这样?

% 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}

该图显示了上述代码的结果

相关内容