3

I am trying to extract the coordinate of an intersection point (Int1) and add it to a plot as follows:

\documentclass{standalone}
\usepackage{amsmath}
\usepackage{tikz, pgfplots}
\usetikzlibrary{calc, intersections}
\pgfplotsset{compat=newest}
\def\cdf(#1)(#2)(#3){0.5*(1+(erf((#1-#2)/(#3*sqrt(2)))))}%
% to be used: \cdf(x)(mean)(variance)

    \begin{document}
    \begin{tikzpicture}
    \begin{axis}[%
      xlabel=$t$,
      ylabel=$\Phi(t)$,
      xmin=1,
      xmax=8,
      ymin=0,
      ymax=1.1,
      grid=none,
      legend entries={CDF, A},legend pos=north west, legend style={draw=none}, legend cell align={left}
      ]
      \def\mean{4};
      \def\var{1};
      %
      \addplot[smooth, thick, red, name path global=cdf]  gnuplot{\cdf(x)(\mean)(\var)};
      %
      %% Draw horizontal line to intersect cdf, then drop perp onto xaxis
      \path[name path global = lx1] (0, 0.6) --  (5, 0.6);
      \draw[dashed, gray, name intersections={ of=cdf and lx1, by = {Int1} }] (Int1)  (0, 0.6) -- (Int1);
      \draw[dashed, gray] (Int1) --  (Int1|-{(0, 0)}); % the curly protects the ()

      %% Does not work
      % \addplot[only marks, red, mark=*] coordinates \pgfplotspointgetcoordinates{Int1};

      % Works
      \addplot[only marks, red, mark=*] coordinates {(4.2, 0.6)};

    \end{axis}
    \end{tikzpicture}
    \end{document}

I have pgfplots 1.16, Texlive 2018, shell escape is enabled. Please help. Edit: I have looked the manual but the only use cases are where it is being used to label a node. I want to know how to use the coordinates in a plot.

0
1

These are expansion issues, I think. That is, the coordinates are not yet "done" when they are needed. (BTW, you'd also have to add brackets: \addplot[only marks, red, mark=*] coordinates {\pgfplotspointgetcoordinates{(Int1)}}; but this does not fix the issue.) Here is a workaround in which I draw an empty plot to get the legend entry and add the plot mark as a node.

\documentclass{standalone}
\usepackage{amsmath}
\usepackage{tikz, pgfplots}
\usetikzlibrary{calc, intersections}
\pgfplotsset{compat=newest}
\def\cdf(#1)(#2)(#3){0.5*(1+(erf((#1-#2)/(#3*sqrt(2)))))}%
% to be used: \cdf(x)(mean)(variance)

    \begin{document}
    \begin{tikzpicture}
    \begin{axis}[%
      xlabel=$t$,
      ylabel=$\Phi(t)$,
      xmin=1,
      xmax=8,
      ymin=0,
      ymax=1.1,
      grid=none,
      legend entries={CDF, A},
      legend pos=north west, legend style={draw=none}, legend cell align={left}
      ]
      \def\mean{4};
      \def\var{1};
      %
      \addplot[smooth, thick, red, name path global=cdf]  gnuplot{\cdf(x)(\mean)(\var)};
      %
      %% Draw horizontal line to intersect cdf, then drop perp onto xaxis
      \path[name path global = lx1] (0, 0.6) --  (5, 0.6);
      \draw[dashed, gray, name intersections={ of=cdf and lx1, by = {Int1} }] (Int1)  (0, 0.6) -- (Int1);
      \draw[dashed, gray] (Int1) --  (Int1|-{(0, 0)}); % the curly protects the ()
      \node[red] at (Int1) {\pgfuseplotmark{*}};
      \addplot[draw=none,only marks=mark=*,red] coordinates {(0,0)};
    \end{axis}
    \end{tikzpicture}
    \end{document}

enter image description here

2
  • I already have this workaround, I posted the question to know what kind of object is \pgfplotspointgetcoordinates{(Int1)} and how do I use it ? – me10240 May 8 '18 at 4:42
  • @me10240 Well, what is different from your workaround is that the coordinates are not hard coded. I really think that you cannot use these coordinates in a plot in the same axis environment. This has to do with the order in which pfgplots expands things. – user121799 May 8 '18 at 4:47
1

I think you're misunderstanding the \pgfplotspointgetcoordinates macro, in that it doesn't return a coordinate pair directly. What it does is to set the value of the keys /data point/x and /data point/y, and to retrieve a value you have to do e.g. \pgfkeysvalueof{/data/point x}. So you cannot use it the way you're trying to use it. I don't know how one could obtain those values for use in a plot though, my attempts failed.

Another workaround could be to do \draw [red] plot[only marks, mark=*] coordinates {(Int1)}; and use \addlegendimage to get the legend entry. Output is same as marmot's answer.

\documentclass{standalone}
\usepackage{amsmath}
\usepackage{pgfplots}
\usetikzlibrary{intersections}
\pgfplotsset{compat=newest}
\def\cdf(#1)(#2)(#3){0.5*(1+(erf((#1-#2)/(#3*sqrt(2)))))}%
% to be used: \cdf(x)(mean)(variance)

\begin{document}
\begin{tikzpicture}[
  intmark/.style={ % for convenience
    mark options={red},only marks,mark=*
  }
]
\begin{axis}[%
  xlabel=$t$,
  ylabel=$\Phi(t)$,
  xmin=1,
  xmax=8,
  ymin=0,
  ymax=1.1,
  grid=none,
  legend entries={CDF, A},
  legend pos=north west,
  legend style={draw=none},
  legend cell align={left},
]

  \newcommand\mean{4};
  \newcommand\var{1};
      %
  \addplot[thick, red, name path global=cdf, domain=1:5]  gnuplot{\cdf(x)(\mean)(\var)};
  \addlegendimage{intmark}


  \path[name path global = lx1] (\pgfkeysvalueof{/pgfplots/xmin}, 0.6) --  (\pgfkeysvalueof{/pgfplots/xmax}, 0.6);


  \draw[dashed, gray, name intersections={ of=cdf and lx1, by = {Int1} }] 
    (Int1 -| current axis.origin) -| (Int1 |- current axis.origin);
  \draw  plot[intmark] coordinates {(Int1)};


\end{axis}
\end{tikzpicture}
\end{document}

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.