Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

In axis environment I have a curve and a line intersecting it. How can I get components of intersection-1, intersection-2 etc. in axis units?

\documentclass{minimal} 
\usepackage{pgfplots}
\usetikzlibrary{intersections}

\begin{document} 

\begin{tikzpicture} 
\begin{axis}
\addplot[name path global=GaussCurve] gnuplot[domain=48.00:56.00,samples=100] {exp(-0.5*((x-52.64)/1.82)**2)/(sqrt(2*pi)*1.82)};
\path[name path global=HelperLine] (axis cs:48,0.13288) -- (axis cs:56,0.13288);
\draw[dashed,name intersections={of=GaussCurve and HelperLine}] (axis cs:48,0.13288) -- (intersection-2);
\fill[red] (intersection-2) circle (.1cm);
\end{axis} 
\end{tikzpicture} 
\end{document}

e.g. how to get X component of red circle (intersection-2)

This code generated by program from some data, so I can't use the presented values, it's just example.

share|improve this question
4  
Please provide a minimum working example, a smallest possible compilable example that shows the exact problem. Like this, it's easier for others to help you. –  gerrit Dec 16 '11 at 9:36
3  
You can use \pgfgetlastxy{\macrox}{\macroy}. This will set \macrox and \macroy to the x and y component of the last used coordinate respectively. So \path (intersection-2); \pgfgetlastxy{\macrox}{\macroy} will set \macrox to the x coordinate of (intersection-2). –  Roelof Spijker Dec 16 '11 at 10:03
add comment

2 Answers

up vote 11 down vote accepted

As wh1t3 said in the comment, you can extract the coordinate using \pgfgetlastxy{<macro for x>}{<macro for y>}. In order to transform this into axis units, you have to apply the inverse of the coordinate transformation that PGFplots uses. In the example below, I've wrapped the transformation in a macro \transformxdimension, which takes a length in pt and sets \pgfmathresult to contain the length in axis units:

\documentclass{minimal} 
\usepackage{pgfplots}
\usetikzlibrary{intersections}

\begin{document} 

\makeatletter
\newcommand\transformxdimension[1]{
    \pgfmathparse{((#1/\pgfplots@x@veclength)+\pgfplots@data@scale@trafo@SHIFT@x)/10^\pgfplots@data@scale@trafo@EXPONENT@x}
}
\newcommand\transformydimension[1]{
    \pgfmathparse{((#1/\pgfplots@y@veclength)+\pgfplots@data@scale@trafo@SHIFT@y)/10^\pgfplots@data@scale@trafo@EXPONENT@y}
}
\makeatother

\begin{tikzpicture} 
\begin{axis}[yticklabel style={/pgf/number format/.cd, fixed, fixed zerofill}]
\addplot[name path global=GaussCurve] gnuplot[domain=48.00:56.00,samples=100] {exp(-0.5*((x-52.64)/1.82)**2)/(sqrt(2*pi)*1.82)};
\path[name path global=HelperLine] (axis cs:48,0.13288) -- (axis cs:56,0.13288);

\draw[dashed,name intersections={of=GaussCurve and HelperLine}] (axis cs:48,0.13288) -- (intersection-2)
    node [anchor=south, fill=white, fill opacity=0.75,text opacity=1]{
        \pgfgetlastxy{\macrox}{\macroy}
        \transformxdimension{\macrox}
        \pgfmathprintnumber{\pgfmathresult},%
        \transformydimension{\macroy}%
        \pgfmathprintnumber{\pgfmathresult} 
    }
;   
\fill[red] (intersection-2) circle (.1cm);
\end{axis} 
\end{tikzpicture} 
\end{document}
share|improve this answer
1  
Fine answer, I can update my code but is it possible with pgfplots to use another tool that node or pgfextra? –  Alain Matthes Dec 17 '11 at 17:09
    
@Altermundus: You should enclose all your \draw, \path, \pgfgetlastxy, etc., in \pgfplotsextra{...}, which will execute all the commands in the same scope. That way, you don't need to \global\let the result, and you don't have to smuggle the \pgfgetlastxy into a \draw command using \pgfextra. Instead, you can just say \pgfplotsextra{ \draw [name intersections=...] ... ; \pgfgetlastxy{...} }. –  Jake Dec 19 '11 at 6:27
    
thanks for the explanations –  Alain Matthes Dec 19 '11 at 6:32
add comment

I try the idea of whlt3 but it's was not easy; see the next code (perhaps I do some wrong things because I don't know very well pgfplots). I try also \pgfextractx. I need in each case to use \pgfextra to get the x component.

Update with the excellent answer of Jake :

  \documentclass{minimal} 
  \usepackage{tikz,pgfplots}
  \usetikzlibrary{intersections}

  \makeatletter
  \newcommand\transformxdimension[1]{
      \pgfmathparse{((#1/\pgfplots@x@veclength)+\pgfplots@data@scale@trafo@SHIFT@x)/%
       10^\pgfplots@data@scale@trafo@EXPONENT@x}
  }
  \newcommand\transformydimension[1]{
    \pgfmathparse{((#1/\pgfplots@y@veclength)+\pgfplots@data@scale@trafo@SHIFT@y)/%
       10^\pgfplots@data@scale@trafo@EXPONENT@y}
   }
 \makeatother

\begin{document} 

\begin{tikzpicture} 
\begin{axis}
\addplot[name path global=GaussCurve] gnuplot[domain=48.00:56.00,samples=100] {%
       exp(-0.5*((x-52.64)/1.82)**2)/(sqrt(2*pi)*1.82)};
\path[name path global=HelperLine] (axis cs:48,0.13288) -- (axis cs:56,0.13288);
\draw[dashed,name intersections={of=GaussCurve and HelperLine,name=i}] (axis %
      cs:48,0.13288) -- (i-2)%
\pgfextra{\pgfgetlastxy{\macrox}{\macroy}%
         \global\let\macrox\macrox};
\fill[red] (i-2)  circle (.1cm);
\draw[dashed] (i-2)--(\macrox,0) node {%
         \transformxdimension{\macrox}
        \pgfmathprintnumber{\pgfmathresult}}; 
\end{axis}

\end{tikzpicture} 


\end{document}

enter image description here

share|improve this answer
    
I don't know to transform the result with axis units –  Alain Matthes Dec 16 '11 at 13:40
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.