0

When using pgfplots, the command \pgfgetlastxy works well without axis environment and with axis environment set as default.

But it is hard getting the correct coordinate of intersecting, when setting something with the axis environment.

enter image description here

I want to get the result with the MWE below.

\documentclass{standalone}

%\usepackage{amsthm,amssymb}
\usepackage{pgfplots}
\usetikzlibrary{arrows,intersections,calc}

\begin{document}
\begin{tikzpicture}[font=\footnotesize]
\begin{axis}[%
axis lines*=none,axis y line=center, axis x line=center,
xlabel=$x$,ylabel=$y$,
every axis x label/.style={at={(current axis.right of origin)},anchor=north east,xshift=1mm},
every axis y label/.style={at={(current axis.above origin)},anchor=north east,yshift=1mm},
xmin=-0.2,ymin=-0.15,
xmax=1.5,ymax=1.5,
%xtick={1},ytick={1},
restrict y to domain=0:1.2,
height=6cm,width=5cm,
enlargelimits=false]
\node[below left,black] at (axis cs:0,0) {$O$};

\addplot[name path global=x2,blue,smooth,samples=500,domain=0:1.2] %
        {x^2} node[pos=0.75,sloped,yshift=-6pt]{$y=x^2$};
\addplot[name path global=genx,red,smooth,samples=500,domain=0:1.2] %
        {sqrt(x)} node[pos=0.75,sloped,yshift=6pt]{$x=y^2$};

\def\vlvalue{0.4}; %x  interval:[x,x+dx]
\def\intvlen{0.1}; %dx
\path[draw=none,name path=lx] (axis cs:\vlvalue,0)--(axis cs:\vlvalue,1.2); %x
\path[draw=none,name path=ldx] (axis cs:\vlvalue+\intvlen,0)--(axis cs:\vlvalue+\intvlen,1.2);%x+dx
%
\path[name intersections={of=x2 and lx,name=k}] (axis cs:\vlvalue,0)--(k-1); 
\path(k-1)  \pgfextra{\pgfgetlastxy{\kx}{\ky}     %%no ;
              \global\let\kx\kx %
              \global\let\ky\ky};
\draw[blue,dashed] (axis cs:\vlvalue,0)--(\kx,\ky);
\path[name intersections={of=genx and lx,name=m}] (axis cs:\vlvalue,0)--(m-1);
\path(m-1)  \pgfextra{\pgfgetlastxy{\mx}{\my}     %%no ;
              \global\let\mx\mx %
              \global\let\my\my};
%draw from (k-1) to (\vlvalue+\intvlen,\ky)
\draw[red] (\kx,\ky)--(axis cs:\vlvalue+\intvlen,\ky); % % %wrong \ky
%draw rectangle 
\fill[cyan!5] (k-1) rectangle (axis cs:\vlvalue+\intvlen,\my); % %wrong \my
\end{axis}
\end{tikzpicture}
\end{document}
  • Could you explain what you are trying to achieve? – Jake Dec 9 '13 at 12:24
  • i want to draw a rectangle \fill[cyan!5] (k-1) rectangle (axis cs:\vlvalue+\intvlen,\my); – huizhan wang Dec 9 '13 at 12:34
3

You don't need \pgfgetlastxy for this, you can work directly with the intersections and with the calc library:

\documentclass{standalone}

%\usepackage{amsthm,amssymb}
\usepackage{pgfplots}
\usetikzlibrary{arrows,intersections,calc}

\begin{document}
\begin{tikzpicture}[font=\footnotesize]
\begin{axis}[%
axis lines*=none,axis y line=center, axis x line=center,
xlabel=$x$,ylabel=$y$,
xmin=-0.2,ymin=-0.15,
xmax=1.5,ymax=1.5,
restrict y to domain=0:1.2,
height=6cm,width=5cm,
enlargelimits=false]
\node[below left,black] at (axis cs:0,0) {$O$};

\addplot[name path global=x2,blue,smooth,samples=500,domain=0:1.2] %
        {x^2} node[pos=0.75,sloped,yshift=-6pt]{$y=x^2$};
\addplot[name path global=genx,red,smooth,samples=500,domain=0:1.2] %
        {sqrt(x)} node[pos=0.75,sloped,yshift=6pt]{$x=y^2$};

\def\vlvalue{0.4}; %x  interval:[x,x+dx]
\def\intvlen{0.1}; %dx
\path[name path=lx] (axis cs:\vlvalue,0)--(axis cs:\vlvalue,1.2);

\draw [
    red,
    name intersections={of=x2 and lx,name=k},
    name intersections={of=genx and lx,name=m}
    ] (k-1) rectangle ($(m-1)+(axis direction cs:\intvlen,0)$); % %wrong \my
\end{axis}
\end{tikzpicture}
\end{document}
  • jake, thank you, you must be a magician! i study your answers for a day, and cannot get the coordinate of intersection. but how get the correct \ky, \my etc.? – huizhan wang Dec 9 '13 at 12:48
  • @huizhanwang: Take a look at Coordinates of intersections. You hardly ever need that approach for drawing something, though. It's mainly useful for labelling intersections. – Jake Dec 9 '13 at 12:54

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.