如何使用 pgfplots 绘制图形

如何使用 pgfplots 绘制图形

在此处输入图片描述

我想绘制这样的图表来解释我的研究路径。我使用包:pg​​fplots、pgfplotstable、tikz 找到了类似的图表。

 \begin{tikzpicture}
    \begin{axis}[title={\texttt{研究路径}},xmin=0]
    \addplot {x^2};
    \end{axis}
\end{tikzpicture}

上述代码生成的图是: 在此处输入图片描述

答案1

与以下答案相反斯蒂芬这里我提出了一个真正使用 PGFPlots 的解决方案。此外,我还提供了除使用该intersections库设置/查找“关键点”之外的两种可能性。

有关更多详细信息,请查看代码中的注释。

% used PGFPlots v1.14
\documentclass[border=5pt]{standalone}
\usepackage{pgfplots}
    % load some libraries
    \usetikzlibrary{
        % needed to calculate the "in-between" ticklabels
        calc,
        % needed for "method 3" (which will be explained later)
        intersections,
    }
    \pgfplotsset{
        % use this `compat' level or higher so TikZ coordinates don't need to
        % be prefixed by `axis cs:'
        compat=1.11,
        % declare the function that you want to plot
        % (this is useful for "method 2" which will be explained later)
        /pgf/declare function={
            f(\x) = \x^2;
        },
    }
\begin{document}
\begin{tikzpicture}[
    % make the font (everywhere) a bit smaller
    font=\small,
]
    \begin{axis}[
        % don't use the standard "boxed" axis environment
        axis lines=middle,
        % we don't need normal ticks
        xtick=\empty,
        ytick=\empty,
        % state the axis labels ...
        xlabel=time,
        ylabel=path,
        % ... and change a bit the positions of them
        xlabel style={
            anchor=north east,
        },
        ylabel style={
            anchor=north east,
        },
        % we want a bit enlarged x limit so the last xticklabel and xlabel
        % don't overlap as well as the last point label isn't clipped
        enlarge x limits={rel=0.3,upper},
        % we don't need markers on the plot
        no markers,
        % set the domain in which the function is evaluated
        domain=0:5,
    ]

        % create a dummy coordinate at the origin which is used to later
        % draw the axis tick labels
        \coordinate (x0) at (0,0);

        % plot the function that we have created earlier
        \addplot+ [
            -latex,
            % give a name to later use it with the `intersections' library
            name path=function,
        ] {f(x)}
%        % alternatively you can write your formula directly here
%        ] {x^2}
            % method 1:
            % directly give coordinates at certain positions of the plot
                % but first list the options that are in common for the coordinates
                [
                    every label/.append style={
                        black,
                    },
                    label position=right,
                ]
            coordinate [
                % at which position of the plot shall the coordinate be created
                pos=0.2,
                % give a label to the coordinate
                label=key point 1,
            ] (kp1)
            % now you could state the next coordinate(s) ...
%            coordinate [
%                pos=0.5,
%                label=key point 2,
%            ] (kp2)
%           % ...
        ;

        % method 2:
        % create coordinates at given x values also using the above created function
        % (this is quite similar to method 1 but here you can state exact x values
        %  if needed)
            % so there is only one place to state the x value, we create a
            % variable to store this value
            \pgfmathsetmacro{\kpTwo}{3.5}
        \coordinate [
            label=right:key point 2,
        ] (kp2) at (\kpTwo,{f(\kpTwo)});

        % method 3:
        % create an invisible path that intersects the plot ...
            \pgfmathsetmacro{\kpThree}{4.5}
        \path [name path=vertical line]
            % to not need to adapt the values manually by changing the
            % axis y limits we just "call" the values
            (\kpThree,\pgfkeysvalueof{/pgfplots/ymin}) --
            (\kpThree,\pgfkeysvalueof{/pgfplots/ymax})
        ;
        % ... and "find" the intersection to create the coordinate
        \path [
            name intersections={%
                of=function and vertical line,
            },
        ] (intersection-1)
            coordinate [label=right:key point 3] (kp3)
        ;

        % draw the horizontal and vertical lines
        % in addition place some dummy labels at the intersecting y and y
        % axis to later draw the corresponding tick labels
        \pgfplotsforeachungrouped \point in {1,2,3} {
            \edef\temp{\noexpand%
                \draw [help lines]
                    (0,0 |- kp\point)
                        coordinate (y\point)
                    -- (kp\point)
                    -- (0,0 -| kp\point)
                        coordinate (x\point)
                ;
            }\temp
        }

        % we need one more dummy coordinate at `xmax` for the axis tick labels
        % instead of using the `\pgfkeysvalueof` feature again now I use
        % another coordinate system
        \coordinate (xn) at (xticklabel* cs:1);
    \end{axis}

    % now we create the ticklabels using some loops
    % (this has to be done outside the `axis' environment when the `\foreach'
    %  command is used. Otherwise you will get an error message)
    % we start with the xticklabels
    \foreach \i [
        remember=\i as \lasti (initially 0),
        count=\x from 0,
    ] in {1,...,3,n} {
        \node [below] at ($ (x\lasti)!0.5!(x\i) $) {$t_{\x}$};
    }

        % for the yticklabels we need another dummy coordinate "y0" to have a
        % simple loop again
        \coordinate (y0) at (x0);
    \foreach \i/\Text [
        remember=\i as \lasti (initially 0),
    ] in {
        1/{merger \\ reorganization},
        2/{integrate \\ resources},
        3/{integrate \\ service}%
    } {
        \node [left,align=right] at ($ (y\lasti)!0.5!(y\i) $) {\Text};
    }

\end{tikzpicture}
\end{document}

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

答案2

这是基于纯 Tikz 的解决方案。我使用抛物线作为函数,因为代码中给出了它。坐标pos1pos2pos3是虚线在 x 轴上的位置。在循环中,y 轴上的相应位置被创建为ypos1ypos2和 ,以及ypos3曲线上的位置inter1inter2inter3

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{calc,arrows,intersections}
\begin{document}
\begin{tikzpicture}[>=triangle 90,x=3cm,y=1.5cm]
  \draw[->](0,0)--(0,4.5)node[anchor=north east]{Path};
  \draw[->](0,0)--(3,0)node[anchor=north west]{Time};
  \draw[->,thick,name path=para](0,0) parabola bend (0,0) (2.5,4);
  \coordinate(pos1) at (1.2,0);
  \coordinate(pos2) at (1.85,0);
  \coordinate(pos3) at (2.35,0);
  \foreach \num in {1,2,3}{
    \path[name path=line\num](pos\num)--+(0,5);
    \path[name intersections={of=para and line\num}](intersection-1) coordinate(inter\num);
    \coordinate (ypos\num) at (inter\num -| {(0,0)});
    \draw[dashed] (pos\num) |- (ypos\num);
    \node[anchor=west] at (inter\num){key point \num};
  }
  \node[below] at ($(0,0)!0.5!(pos1)$){T0};
  \node[below] at ($(pos1)!0.5!(pos2)$){T1};
  \node[below] at ($(pos2)!0.5!(pos3)$){T2};
  \node[below] at ($(pos3)!0.4!(3,0)$){T3};
  %%
  \node at ($(0,0)!0.5!(ypos1)$){Merger reorganization};
  \node at ($(ypos1)!0.5!(ypos2)$){Integrate resources};
  \node at ($(ypos2)!0.5!(ypos3)$){Integrate service};
\end{tikzpicture}
\end{document}

在此处输入图片描述

相关内容