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.

This is a follow-up question to: Generalised: Accessing the logic values of a TikZ coordinate.

In that question a PGFplot answer was suggested by jake.

I took that answer and modified it to the following MWE:

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

 \newcommand\xcoord[2][center]{{%
   \pgfpointanchor{#2}{#1}%
   \pgfgetlastxy{\ix}{\iy}%
   \pgfplotspointaxisorigin%
   \pgfgetlastxy{\ox}{\oy}
   \pgfmathparse{(\ix-\ox)/\pgfplotsunitxlength/1000}
   \pgfmathprintnumber{\pgfmathresult}}
}

\begin{document}

\vspace{1cm}
\begin{tikzpicture}
\begin{axis}[
  domain=1:8.2,
  samples=150,
  no markers,
  axis lines=middle,
  enlarge x limits=upper,
  enlarge y limits=true,
  x axis line style={{name path global=xaxis}}
  ]
\addplot +[name path global=plot] {(x-2.07)*(x-4.09)*(x-5.72)*(x-8.04)};
\pgfplotsextra{ 
\fill [name intersections={of=xaxis and plot, name=i, total=\t}] 
  [red, every node/.style={black}] 
  (i-1) circle (2pt) node [pin={\xcoord{i-1}}] {}
  (i-2) circle (2pt) node [pin={\xcoord{i-2}}] {};
}
\end{axis}
\end{tikzpicture}
\end{document} 

This produces the following:

The result

So the coordinates is off by a factor 10.

How would one correct that and I was wondering how one would access them through a foreach loop in stead of specifically making a node for each of them.

share|improve this question
add comment

1 Answer

up vote 4 down vote accepted

Hm, something's not quite right with the \pgfplotsunitxlength macro. I've written a new macro \xcoord macro that works by extracting the values for the plot points (0,0) and (1,1) and using that for the unit vector. Note also that \pgfplotspointorigin points to the point where the two axes meet, not to the point (0,0), so the value was not only off by a factor of 10, but also by a shift of 1. I used the point (0,0) in the code below to correct for this.

You can just loop through the intersections by using \foreach \n in {1,...,\t}, where \t is the macro that holds the total number of intersections specified in total=\t.

There are sometimes issues with intersections being found twice if they lie exactly on one of the support points of the plot. In that case, you should adjust the domain slightly.

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

 \newcommand\xcoord[2][center]{{%
   % The actual point of interest
   \pgfpointanchor{#2}{#1}%
   \pgfgetlastxy{\ix}{\iy}%
   % (0,0)
   \pgfplotspointaxisxy{0}{0}%
   \pgfgetlastxy{\ox}{\oy}
   % (1,1)
   \pgfplotspointaxisxy{1}{1}%
   \pgfgetlastxy{\ux}{\uy}
   \pgfmathparse{(\ix-\ox)/(\ux-\ox)}
   \pgfmathprintnumber{\pgfmathresult}}
}

\begin{document}

\vspace{1cm}
\begin{tikzpicture}
\begin{axis}[
  xmin=0,
  domain=0:8.4,
  restrict y to domain=-100:50,
  samples=149,
  no markers,
  axis lines=middle,
  enlarge x limits=upper,
  enlarge y limits=true,
  x axis line style={{name path global=xaxis}}
  ]
\addplot +[name path global=plot] {(x-2.07)*(x-4.09)*(x-5.72)*(x-8.04)};
\pgfplotsextra{ 
\fill [name intersections={of=xaxis and plot, name=i, total=\t},
    red,
    every node/.style={black}] 
    \foreach \n in {1,...,\t} {
  (i-\n) circle (2pt) node [pin={\xcoord{i-\n}}] {}
  };
}
\end{axis}
\end{tikzpicture}
\end{document} 

pgfplots intersections logical coordinates

share|improve this answer
    
TNX it works exactly as you indicated. –  Louis Jul 24 '11 at 17:22
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.