如何在图上的坐标处绘制箭头?

如何在图上的坐标处绘制箭头?

我试图绘制下面的图表 在此处输入图片描述

我的 MWE 是

\documentclass{standalone}

\usepackage{pgfplots}

\begin{document}
  \begin{tikzpicture}
    \begin{axis}[
        restrict y to domain=-1:4,
        samples=1000,
        ticks=none,
        xmin = -1, xmax = 4,
        ymin = -1, ymax = 4,
        unbounded coords=jump,
        axis x line=middle,
        axis y line=middle,
        xlabel={$x$},
        ylabel={$y$},
        x label style={
          at={(axis cs:4.02,0)},
          anchor=west,
        },
        every axis y label/.style={
          at={(axis cs:0,4.02)},
          anchor=south
        },
        legend style={
          at={(axis cs:-5.2,4)},
          anchor=west, font=\scriptsize
        }
        ] 
      \addplot[very thick,color=blue, mark=none, domain=-4:3.5] {1+(x-1)^3};
      \addplot[mark=*,fill=white] coordinates {(2,2)};
      \node at (axis cs:2.5,3.5) [anchor=west] {\color{blue}$f(x)$};
    \end{axis}
    \draw[dashed] (4,3.4)--(1.4,3.4) node[left] {$L$};
    \draw[dashed] (4.12,3.3)--(4.12,1.15) node[below] {$a$};
  \end{tikzpicture}
\end{document} 

我的问题是:把箭头放到合适的位置。

笔记:我对我的代码不太满意(虚线是通过常规方式获得的)。任何改进都值得赞赏。

答案1

最直接的方法可能是将情节分成两部分,然后在情节设置中\addplot添加->或。我还对情节进行了一些编辑,以便箭头尖端不会被标记覆盖。<-shorten

在 内也画出虚线axis,这样就无需猜测坐标了。然后给$a$$L$节点命名,并绘制相对于这些节点的箭头。

代码输出

\documentclass[border=5mm]{standalone}

\usepackage{pgfplots}

\begin{document}
  \begin{tikzpicture}
    \begin{axis}[
        restrict y to domain=-1:4,
        samples=100, % you don't need 1000, it only slows things down
        ticks=none,
        xmin = -1, xmax = 4,
        ymin = -1, ymax = 4,
        unbounded coords=jump,
        axis x line=middle,
        axis y line=middle,
        xlabel={$x$},
        ylabel={$y$},
        x label style={
          at={(axis cs:4.02,0)},
          anchor=west,
        },
        every axis y label/.style={
          at={(axis cs:0,4.02)},
          anchor=south
        },
        legend style={
          at={(axis cs:-5.2,4)},
          anchor=west, font=\scriptsize
        },
        declare function={f(\x)=1+(\x-1)^3;},
        ] 
      \addplot[very thick,color=blue, mark=none, domain=-4:2, ->,shorten >=1pt] {f(x)};
      \addplot[very thick,color=blue, mark=none, domain=2:3.5, <-,shorten <=1pt] {f(x)}
          node [right=3mm,near end] {$f(x)$};
      \addplot[mark=*,fill=white] coordinates {(2,{f(2)})};

      \draw[dashed] (axis cs:0,{f(2)}) node[left=1mm] (l) {$L$} -| 
                    (axis cs:2,0) node[below] (a) {$a$};       
    \end{axis}
    \draw [<-] (l) -- ++(0,7mm) node [above] {$f(x)$};
    \draw [<-] (l) -- ++(0,-7mm) node [below] {$f(x)$};
    \draw [<-] (a) -- ++(-7mm,0) node [left] {$x$};
    \draw [<-] (a) -- ++(7mm,0) node [right] {$x$};
  \end{tikzpicture}
\end{document} 

答案2

或者:

\documentclass[margin=3mm]{standalone}
\usepackage{pgfplots}
\pgfplotsset{compat=1.15}
\usetikzlibrary{arrows.meta}

\begin{document}
  \begin{tikzpicture}
    \begin{axis}[
        restrict y to domain=-1:4,
        samples=100,
        ticks=none,
        xmin = -1, xmax = 4,
%        ymin = -1, ymax = 4,
%        unbounded coords=jump,
        axis  lines=middle,
%        axis y line=middle,
        xlabel={$x$},
        ylabel={$y$},
        x label style={anchor=west},
        y label style={anchor=south},
%        legend style={
%          at={(axis cs:-5.2,4)},
%          anchor=west, font=\scriptsize
%        }
        mark=none,
        ]
      \addplot[very thick,color=blue, domain=-4:1.99,  -Straight Barb] {1+(x-1)^3};
      \addplot[very thick,color=blue, domain= 2.01:3.5,Straight Barb-] {1+(x-1)^3};
      \addplot[densely dashed] coordinates {(2,2) (0,2)}
                node[left] {$\begin{array}{c@{}}    \color{blue}f(x)\\
                                                    \downarrow\\
                                                    L\\
                                                    \uparrow\\
                                                    \color{blue}f(x)
                              \end{array}$};
      \addplot[densely dashed] coordinates {(2,2) (2,0)}
                node[below] {$x\to a \gets x$};
      \addplot[mark=*,fill=white] coordinates {(2,2)};
    \end{axis}
  \end{tikzpicture}
\end{document}

笔记:使用它绘图时pgfplots建议说明所\pgfplotsset{compat=1.15}用的是哪个版本。

在此处输入图片描述

答案3

另一种选择是元帖子,包含有用的cutbeforecutafter

在此处输入图片描述

\RequirePackage{luatex85}
\documentclass[border=5mm]{standalone}
\usepackage{luamplib}
\begin{document}
\mplibtextextlabel{enable}
\begin{mplibcode}
beginfig(1);
    path xx, yy, ff;
    vardef f(expr x) = 1+(x-1)**3 enddef;

    numeric a,b,s,u; 
    a = -1;
    b = 4;
    s = 1/8;
    u = 1cm;

    xx = ((a,0)--(b,0)) scaled u;
    yy = ((0,a)--(0,b)) scaled u;

    ff = ((a, f(a)) for x=a+s step s until b: .. (x, f(x)) endfor)
         scaled u
         cutbefore xx shifted (0,a*u)
         cutafter  xx shifted (0,b*u);

    drawarrow xx;
    drawarrow yy;

    z0 = (2, f(2)) scaled u;

    draw (0,y0) -- z0 -- (x0,0) dashed evenly scaled 1/2;

    interim ahangle := 30;
    drawarrow ff         cutafter halfcircle rotated 180 scaled 4 shifted z0 withcolor .37 green;
    drawarrow reverse ff cutafter halfcircle             scaled 4 shifted z0 withcolor .37 green; 
    fill fullcircle scaled 3 shifted z0 withcolor white;
    draw fullcircle scaled 3 shifted z0;

    label.bot("$x \rightarrow a \leftarrow x$", (x0,0));
    label.lft("\hbox{\vbox{\halign{\hss$#$\hss\cr f(x)\cr\downarrow\cr L\cr\uparrow\cr f(x)\cr}}}", (0,y0));
    label.rt("$f(x)$", point infinity of ff) withcolor .37 green;

endfig;
\end{mplibcode}
\end{document}

答案4

这里有一个使用 pstricks 的解决方案。

在此处输入图片描述

\documentclass[pstricks,border=5mm,x11names,svgnames]{standalone}
\usepackage{pst-plot,pstricks-add}

\begin{document}

\multido{\rA=0.6+0.2}{20}{%
\begin{pspicture}[saveNodeCoors,NodeCoorPrefix=n,algebraic](-1,-1)(5,3)
\def\leftAr{0}\def\rightAr{19}
\def\funcRP{t t 0.5 mul 1 sub 3 exp 1 add}
\def\funcAlg{1+(0.5*x-1)^3}
\pscurvepoints[plotpoints=100]{\rA\space 0.1 sub}{\rA\space 0.1 add}{\funcRP}{P}%
\pspolylineticks[Os=0,Ds=0.01,ticksize=0 0]{P}{ ds }{0}{20}%
\pnode(PTick10){A}
\pnode(PTick\leftAr){B}
\pnode(PTick\rightAr){C}
\psset{xunit=1,plotpoints=500,arrowsize=3pt,arrowinset=0.025,arrowlength=2.5,linewidth=0.6pt}
\psaxes[labels=none,ticks=none,showorigin=false]{->}(0,0)(-1,-1)(5,3)[$x$,-100][$y$,190]
\psCoordinates[showpoints=false,markPoint,linestyle=dashed,dash=2pt 2pt,linecolor=gray,linewidth=0.3pt,dotstyle=Bo](A)
\psplot[linecolor=DodgerBlue1]{-0.35}{nBx}{\funcAlg}
\psplot[linecolor=DodgerBlue1]{nCx}{4.5}{\funcAlg}
\psrline[arrows=->,arrowscale=.75,linecolor=DodgerBlue1](PTick\leftAr)(1.6pt;{(PTangent\leftAr)})
\psrline[arrows=->,arrowscale=.75,linecolor=DodgerBlue1](PTick\rightAr)(-1.5pt;{(PTangent\rightAr)})
\uput{0.2}[-90](!nAx 0){\scriptsize$x \rightarrow a \leftarrow x$}
\uput{0.2}[180](!0 nAy){\shortstack{\scriptsize$f(x)$ \\\scriptsize $\downarrow$ \\\scriptsize $L$ \\\scriptsize $\uparrow$\\\scriptsize $f(x)$}}
\end{pspicture}
}
\end{document} 

这是一个动态的解决方案。

在此处输入图片描述

\documentclass[pstricks,border=5mm,x11names,svgnames,dvipsnames]{standalone}
\usepackage{pst-plot,pstricks-add,xfp,xintexpr}

\begin{document}

\multido{\nA=0+5}{39}%
{%
\begin{pspicture}[saveNodeCoors,NodeCoorPrefix=n,algebraic](-1,-1)(5,3)
%\def\nA{20}
\def\xA{3.3}
\def\nB{\inteval{190 - \nA}}
\def\nC{\inteval{190 - \nA}}
\def\leftAr{10}
\def\rightAr{10}
\def\funcRP{t t 0.5 mul 1 sub 3 exp 1 add}% Parameterform: {t f(t)}
\def\funcAlg{1+(0.5*x-1)^3}
\pscurvepoints[plotpoints=100]{\xA}{\xA\space 0.11 add}{\funcRP}{P}%
\pspolylineticks[Os=0,Ds=0.01,ticksize=0 0]{P}{ ds }{0}{11}%
\pscurvepoints[plotpoints=100]{\xA}{\xA\space 0.11 sub}{\funcRP}{Q}%
\pspolylineticks[Os=0,Ds=0.01,ticksize=0 0]{Q}{ ds }{0}{11}%
\pscurvepoints[plotpoints=100]{\xA}{\xA\space 2 sub}{\funcRP}{PL}%
\pspolylineticks[Os=0,Ds=0.01,ticksize=0 0]{PL}{ ds }{0}{200}%
\pscurvepoints[plotpoints=100]{\xA}{\xA\space 2 add}{\funcRP}{PR}%
\pspolylineticks[Os=0,Ds=0.01,ticksize=0 0]{PR}{ ds }{0}{200}%
\pnode(PLTick\nC){L}
\pnode(PRTick\nB){R}
\pnode(PTick0){A}
\pnode(QTick\leftAr){B}
\pnode(PTick\rightAr){C}
\psset{xunit=1,plotpoints=500,arrowsize=3pt,arrowinset=0.025,arrowlength=2.5,linewidth=0.6pt,framesep=0.4pt}
\psaxes[labels=none,ticks=none,showorigin=false]{->}(0,0)(-1,-1)(5,3)[$x$,-100][$y$,190]
\xintifboolexpr{\nC=0}{}{\psCoordinates[showpoints=false,markPoint,linestyle=dashed,dash=2pt 2pt,linecolor=gray,linewidth=0.3pt,dotstyle=Bo,dotsize=0.1cm](A)}
\psplot[linecolor=DodgerBlue1]{-0.35}{nBx}{\funcAlg}
\psplot[linecolor=DodgerBlue1]{nCx}{4.7}{\funcAlg}
\psrline[arrows=->,arrowscale=.75,linecolor=DodgerBlue1](B)(-1.5pt;{(QTangent\leftAr)})
\psrline[arrows=->,arrowscale=.75,linecolor=DodgerBlue1](C)(-1.5pt;{(PTangent\rightAr)})
\xintifboolexpr{\nC=0}{\psdot[dotsize=1.6pt,linecolor=DodgerBlue3](A)
\pcline[linestyle=dashed,dash=1.5pt 1.5pt,linewidth=0.3pt,nodesepB=1.4pt,linecolor=DodgerBlue3,arrowscale=0.7]{->}(!nAx 0)(A)
\ncput*{\color{DodgerBlue3}\tiny$f(a)$}
\pcline[linestyle=dashed,dash=1.5pt 1.5pt,linewidth=0.3pt,nodesepA=1pt,linecolor=DodgerBlue3,arrowscale=0.7]{->}(A)(!0 nAy)
\psdot[linecolor=gray,linewidth=0.3pt,dotstyle=Bo,dotsize=0.1cm](A)
}%
{%
\psdot[dotsize=1.5pt,linecolor=Green](R)
\psdot[dotsize=1.5pt,linecolor=BrickRed](L)
\pcline[linestyle=dashed,dash=1.5pt 1.5pt,linewidth=0.3pt,nodesepB=0.75pt,linecolor=BrickRed,arrowscale=0.7]{->}(!nLx 0)(L)\ncput*{\tiny$f(x)$}
\pcline[linestyle=dashed,dash=1.5pt 1.5pt,linewidth=0.3pt,nodesepA=0.75pt,linecolor=BrickRed,arrowscale=0.7]{->}(L)(!0 nLy)
\pcline[linestyle=dashed,dash=1.5pt 1.5pt,linewidth=0.3pt,nodesepB=0.75pt,linecolor=Green,arrowscale=0.7]{->}(!nRx 0)(R)\ncput*{\tiny$f(x)$}
\pcline[linestyle=dashed,dash=1.5pt 1.5pt,linewidth=0.3pt,nodesepA=0.75pt,linecolor=Green,arrowscale=0.7]{->}(R)(!0 nRy)
}
\uput{0.2}[-90](!nAx 0){\scriptsize$x \rightarrow a \leftarrow x$}
\uput{0.2}[180](!0 nAy){\shortstack{\scriptsize$f(x)$ \\\scriptsize $\downarrow$ \\\scriptsize $L$ \\\scriptsize $\uparrow$\\\scriptsize $f(x)$}}
\end{pspicture}
}
\end{document} 

相关内容