Pgf-Plot 在节点和函数之间画一条线

Pgf-Plot 在节点和函数之间画一条线

我正在构建一个pgfplots并想在节点和蓝线之间画线。

不知道如何将节点与线路连接。希望有人能帮助我?

\begin{tikzpicture}
\begin{axis}
[ 
xlabel={$x$}, ylabel={$y$}
,axis lines=middle
,xmin=0,xmax=9,ymin=0,ymax=5
,xtick={0,2,...,8}
,ytick={0,1,...,4}
,samples=41, thick
,domain=0:8
,legend pos=outer north east
]
\addplot+[no marks,blue] {0.35*x+1};
\addlegendentry{$g$}
\addplot[only marks,red] coordinates {
    (2,0.8)
    (3,2.4)
    (4,3.2)
    (5,1.8)
    (6,2.8)
    (7,4)
    (8,2.7)
};
\addlegendentry{$gg$}
\end{axis}
\end{tikzpicture}

在此处输入图片描述

答案1

根据nodes near coords和,我提出以下建议scatter/@pre marker code

\documentclass[tikz, border=2mm]{standalone}
\usepackage{pgfplots}
\pgfplotsset{compat=1.16}

\begin{document}

\begin{tikzpicture}
\begin{axis}[
  declare function={f(\x) = 0.35*\x + 1;},
  axis lines=middle,
  xlabel={$x$}, ylabel={$y$},
  x label style={anchor=west, inner sep=2pt},  % <-- added
  y label style={anchor=east, inner xsep=2pt}, % <-- added
  hide obscured x ticks=false,      % added to show the x tick at 0
  hide obscured y ticks=false,      % added to show the y tick at 0
  xmin=0, xmax=9, ymin=0, ymax=4.9, % avoid clash with the y label
  xtick distance=2,                 % as suggested by Stefan Pinnow
  ytick distance=1,                 % ditto
  samples=2, thick,                 % 2 samples are enough for a line
  domain=0:8,
  legend pos=outer north east,
  legend style={font=\small},
  ]

\addplot+[no marks, blue] {f(x)};
\addlegendentry{$y = 0.35x + 1$}

\addplot[
  only marks, mark=*, mark size=1pt, mark options={draw=red, fill=red},
  % This is the important part:
  nodes near coords={},
  scatter/@pre marker code/.append code={
    \draw[help lines]
         (0,0)
      -- (0, {f(\pgfkeysvalueof{/data point/x})-\pgfkeysvalueof{/data point/y}});
  }]
  coordinates {
      (2,0.8)
      (3,2.4)
      (4,3.2)
      (5,1.8)
      (6,2.8)
      (7,4)
      (8,2.7)
  };
\addlegendentry{points}

\end{axis}
\end{tikzpicture}

\end{document}

在此处输入图片描述

也可以使用 来实现scatter/position=absolute,但是会稍微复杂一些:

\documentclass[tikz, border=2mm]{standalone}
\usepackage{pgfplots}
\pgfplotsset{compat=1.16}

\begin{document}

\begin{tikzpicture}
\begin{axis}[
  declare function={f(\x) = 0.35*\x + 1;},
  axis lines=middle,
  xlabel={$x$}, ylabel={$y$},
  x label style={anchor=west, inner sep=2pt},  % <-- added
  y label style={anchor=east, inner xsep=2pt}, % <-- added
  hide obscured x ticks=false,      % added to show the x tick at 0
  hide obscured y ticks=false,      % added to show the y tick at 0
  xmin=0, xmax=9, ymin=0, ymax=4.9, % avoid clash with the y label
  xtick distance=2,                 % as suggested by Stefan Pinnow
  ytick distance=1,                 % ditto
  samples=2, thick,                 % 2 samples are enough for a line
  domain=0:8,
  legend pos=outer north east,
  legend style={font=\small},
  ]

\addplot+[no marks, blue] {f(x)};
\addlegendentry{$y = 0.35x + 1$}

\addplot[
  only marks, forget plot,
  % This is the important part:
  nodes near coords={},
  scatter/position=absolute,
  scatter/@pre marker code/.append code={
    \draw[help lines]
         (\pgfkeysvalueof{/data point/x}, \pgfkeysvalueof{/data point/y})
      -- (\pgfkeysvalueof{/data point/x}, {f(\pgfkeysvalueof{/data point/x})});
    \path[red] plot[mark=*, mark size=1pt, only marks]
      coordinates {(\pgfkeysvalueof{/data point/x},
                    \pgfkeysvalueof{/data point/y})};
  }]
  coordinates {
      (2,0.8)
      (3,2.4)
      (4,3.2)
      (5,1.8)
      (6,2.8)
      (7,4)
      (8,2.7)
  };

\addlegendimage{
  legend image code/.code={
    \draw[red]
      plot[only marks, mark=*, mark size=1pt] coordinates { (0.3cm,0cm) };
  },
}
\addlegendentry{points}

\end{axis}
\end{tikzpicture}

\end{document}

与上面相同的输出。

答案2

这是对(同样)很棒的想法的改进的答案(对于评论来说太长了)维内斯(+1)使用 atable代替coordinates。这让生活变得更加轻松 :)

% used PGFPlots v1.16
\documentclass[border=5pt]{standalone}
\usepackage{pgfplots}
    % create an empty plot mark
    % (which is needed later)
    \pgfdeclareplotmark{noMark}{}
\begin{document}
\begin{tikzpicture}[
    declare function={foo(\x) = 0.35 * \x + 1;}
]
    \begin{axis}[
        xmin=0,xmax=9,ymin=0,ymax=5,
        % (replaced `[x|y]tick`s)
        xtick distance=2,
        ytick distance=1,
        xlabel={$x$},
        ylabel={$y$},
        axis lines=middle,
        hide obscured x ticks=false,
        hide obscured y ticks=false,
        samples=2,     % <-- (more than 2 samples are not needed for a straight line)
        domain=0:8,
        legend pos=outer north east,
        thick,
    ]
        \addplot[blue] {foo(x)};
        \addlegendentry{$g$}

        % add a custom legend entry to imitate a "normal" mark plot
        \addlegendimage{red,only marks,mark=*}
        \addlegendentry{$gg$}

        % since this is the last plot and no more `\addlegendentry` is given
        % it doesn't show up in the legend
        \addplot+ [
            red,
            only marks,
            % don't show the normal mark ...
            mark=noMark,
            error bars/.cd,
                y dir=both,
                y explicit,
                % ... show the mark at the error (position)
                error mark=*,
        % replaced `coordinates` by a table which allows to calculate the
        % appropriate error position easily
        ] table [
            x=x,
            y expr={foo(\thisrow{x})},
            y error plus expr={\thisrow{y} - foo(\thisrow{x})},
        ] {
            x   y
            2   0.8
            3   2.4
            4   3.2
            5   1.8
            6   2.8
            7   4.0
            8   2.7
        };
    \end{axis}
\end{tikzpicture}
\end{document}

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

答案3

我认为我的尝试有点作弊,但它完成了工作。

\documentclass[tikz]{standalone}
\usepackage{pgfplots}

\begin{document}    
\pgfdeclareplotmark{noMark}{}
\begin{tikzpicture}[
declare function={foo(\x) = 0.35 * \x + 1;}
]
\begin{axis}
[ 
xlabel={$x$}, ylabel={$y$}
,axis lines=middle
,xmin=0,xmax=9,ymin=0,ymax=5
,xtick={0,2,...,8}
,ytick={0,1,...,4}
,samples=41, thick
,domain=0:8
,legend pos=outer north east
]
\addplot[blue] {foo(x)};
\addlegendentry{$g$}
\addlegendimage{red,only marks,mark=*}
\addlegendentry{$gg$}
%
\addplot+ [
red,
only marks,
mark=noMark,
error bars/.cd,
y dir=both,
y explicit,
error mark=*,
] coordinates {
    (2,{foo(2)}) += (0,{0.8-foo(2)}) 
    (3,{foo(3)}) += (0,{2.4-foo(3)})
    (4,{foo(4)}) += (0,{3.2-foo(4)})
    (5,{foo(5)}) += (0,{1.8-foo(5)})
    (6,{foo(6)}) += (0,{2.8-foo(6)})
    (7,{foo(7)}) += (0,{4.0-foo(7)})
    (8,{foo(8)}) += (0,{2.7-foo(8)})
};
\end{axis}
\end{tikzpicture}
\end{document}

在此处输入图片描述

相关内容