当使用 pgfplots 时,该命令\pgfgetlastxy
无需轴环境即可正常工作,并且轴环境设置为默认环境。
但是在轴环境中设置某些东西时很难获得正确的相交坐标。
我想使用下面的 MWE 获得结果。
\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}
答案1
您不需\pgfgetlastxy
要这样做,您可以直接使用交叉点和calc
库:
\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}