在 pgfplots 中标记两条曲线的交点

在 pgfplots 中标记两条曲线的交点

我想标记绿色和红色曲线的交点。我试过的所有方法都不起作用。有人能帮我吗?这是我目前的代码。

\usepackage{pgfplots}
\pgfplotsset{compat=1.11}
\usepackage{Tikz}
\usetikzlibrary{intersections}


\begin{document}
    \begin{figure}[H]
      \begin{tikzpicture}
      \begin{axis}[
          width=0.95\textwidth,
          height=0.6\textwidth,
          xlabel={Knotenanzahl},
          ylabel={Ergebnisse in \%},
          axis lines=left,
          xmin=0,
          xmax=370,
          xtick={0,49,100,149,200,249,300,349},
          ymin=0,
          ymax=110,
          thick,
          grid=both,
          legend style={at={(1.05,0.5)},anchor=west}
          ]
      \addplot[color = red,smooth,thick,mark=x,name path=h1] plot coordinates {
          (0,0)
          (49,27.5)
          (99,45.5)
          (149,70.5)
          (199,85.5)
          (249,95)
          (299,98.5)
          (349,100)
      };

      \addplot[color = blue,smooth,thick,mark=x,name path=h2] plot coordinates {
          (0,0)
          (49,18)
          (99,14.5)
          (149,4.5)
          (199,0)
          (249,0)
          (299,0.5)
          (349,0)
      };



      \addplot[color = Green,smooth,thick,mark=x,name path=h3] plot coordinates {
          (0,0)
          (49,54.5)
          (99,40)
          (149,25)
          (199,14.5)
          (249,5)
          (299,1)
          (349,0)
      };

      \fill [name intersections={of=h1 and h3,by=E}] (intersection-2) circle (2.5pt)
            coordinate (a);

      \legend{H1 $<$ H2,H2 $<$ H1,H1 $=$ H2}

      \end{axis}
  \end{tikzpicture}
  \caption{Ergebnisse im Vergleich, $p = 0.25$}
\end{figure}

\end{document}

在此处输入图片描述

答案1

你需要使用类似

\fill [name intersections={of=h1 and h3,by={E1,E2}}] (E2) circle[radius=2.5pt];

我在其中为第二个交叉点添加了一个名称(by用于命名交叉点),并且我切换到语法,circle[radius=<radius>] 因为(弃用的语法)circle(<radius>)不再适用于最新版本的 pgf pgfplots

\documentclass{article}

\usepackage{pgfplots}
\pgfplotsset{compat=1.11}% 1.16 would be better
\usetikzlibrary{intersections}


\begin{document}
    \begin{figure}[htb]
      \begin{tikzpicture}
      \begin{axis}[
          width=0.75\textwidth,
          height=0.6\textwidth,
          xlabel={Knotenanzahl},
          ylabel={Ergebnisse in \%},
          axis lines=left,
          xmin=0,
          xmax=370,
          xtick={0,49,100,149,200,249,300,349},
          ymin=0,
          ymax=110,
          thick,
          grid=both,
          legend style={at={(1.05,0.5)},anchor=west}
          ]
      \addplot[color = red,smooth,thick,mark=x,name path=h1] plot coordinates {
          (0,0)
          (49,27.5)
          (99,45.5)
          (149,70.5)
          (199,85.5)
          (249,95)
          (299,98.5)
          (349,100)
      };

      \addplot[color = blue,smooth,thick,mark=x,name path=h2] plot coordinates {
          (0,0)
          (49,18)
          (99,14.5)
          (149,4.5)
          (199,0)
          (249,0)
          (299,0.5)
          (349,0)
      };



      \addplot[color = green,smooth,thick,mark=x,name path=h3] plot coordinates {
          (0,0)
          (49,54.5)
          (99,40)
          (149,25)
          (199,14.5)
          (249,5)
          (299,1)
          (349,0)
      };


      \legend{H1 $<$ H2,H2 $<$ H1,H1 $=$ H2}
      \fill [name intersections={of=h1 and h3,by={E1,E2}}] (E2) circle[radius=2.5pt];
      \end{axis}
  \end{tikzpicture}
  \caption{Ergebnisse im Vergleich, $p = 0.25$.}
\end{figure}
\end{document}

在此处输入图片描述

如果你想标记交叉点,你可以使用例如

\documentclass{article}

\usepackage{pgfplots}
\pgfplotsset{compat=1.11}% 1.16 would be better
\usetikzlibrary{intersections}


\begin{document}
    \begin{figure}[htb]
      \begin{tikzpicture}
      \begin{axis}[
          width=0.75\textwidth,
          height=0.6\textwidth,
          xlabel={Knotenanzahl},
          ylabel={Ergebnisse in \%},
          axis lines=left,
          xmin=0,
          xmax=370,
          xtick={0,49,100,149,200,249,300,349},
          ymin=0,
          ymax=110,
          thick,
          grid=both,
          legend style={at={(1.05,0.5)},anchor=west}
          ]
      \addplot[color = red,smooth,thick,mark=x,name path=h1] plot coordinates {
          (0,0)
          (49,27.5)
          (99,45.5)
          (149,70.5)
          (199,85.5)
          (249,95)
          (299,98.5)
          (349,100)
      };

      \addplot[color = blue,smooth,thick,mark=x,name path=h2] plot coordinates {
          (0,0)
          (49,18)
          (99,14.5)
          (149,4.5)
          (199,0)
          (249,0)
          (299,0.5)
          (349,0)
      };



      \addplot[color = green,smooth,thick,mark=x,name path=h3] plot coordinates {
          (0,0)
          (49,54.5)
          (99,40)
          (149,25)
          (199,14.5)
          (249,5)
          (299,1)
          (349,0)
      };


      \legend{H1 $<$ H2,H2 $<$ H1,H1 $=$ H2}
      \fill [name intersections={of=h1 and h3,by={E1,E2}}] (E2)
       node[circle,fill,inner sep=2.5pt,label=above:$X$]{};
      \end{axis}
  \end{tikzpicture}
  \caption{Ergebnisse im Vergleich, $p = 0.25$.}
\end{figure}
\end{document}

在此处输入图片描述

用你喜欢的任何标签替换X。请注意,由于你将图例放在了图形之外,因此我不得不将其缩小一点,以免导致水平盒子过满。

相关内容