输出:

输出:

问题

我想给我画的曲线附加一个标签。

我当前的解决方案是在函数图后添加一个节点(见下面的代码)。但是,当我使用选项明确地剪切出图形的一部分时,clip = true标签可以完全远离曲线(见下图)。

您对如何解决这个问题有什么想法吗?或者您对标记曲线的问题有其他解决方法吗?如果可能的话,我正在寻找一种自动解决方案。

图片

在此处输入图片描述

代码

\documentclass{article}
\usepackage[x11names]{xcolor}
\usepackage{tikz}
\usepackage{pgfplots}
\begin{document}



\begin{tikzpicture}[domain=0.01:5]
\begin{axis}
[grid = major, 
clip = true, 
clip mode=individual, 
axis x line = middle, 
axis y line = middle, 
xlabel={$x$}, 
xlabel style={at=(current axis.right of origin), anchor=west}, 
ylabel={$y$}, 
ylabel style={at=(current axis.above origin), anchor=south}, 
domain = 0.01:5, 
xmin = 0, 
xmax = 5, 
enlarge y limits={rel=0.13}, 
enlarge x limits={rel=0.07}, 
ymin = -10, 
ymax = 10, 
after end axis/.code={\path (axis cs:0,0) node [anchor=north west,yshift=-0.075cm] {0} node [anchor=south east,xshift=-0.075cm] {0};}]

\addplot[color=Firebrick2, samples=100, smooth, ultra thick] {x^2}
node(endofplotsquare){} ;
\node [right, color=Firebrick2] at (endofplotsquare) {$x^2$};

\addplot[color=DodgerBlue2, samples=100, smooth, ultra thick] {exp(x)};

\addplot[color=Gold3, samples=1000, smooth, ultra thick, unbounded coords=jump,  no markers] {ln(x)};
\end{axis}

\end{tikzpicture}

\end{document}

答案1

您可以结合使用pgfplots'nodes near coords功能restrict y domain to以避免寻找最后一个未剪辑点的反复试验过程。

通过使用(与和restrict y to domain=-10:10相同的值)丢弃不在该间隔内的任何点。因此,间隔内的最后一个点将始终是,并且节点可以像这样轻松放置:yminymaxpgfplotspos=1

\addplot[color=Firebrick2,samples=100,smooth,ultra thick] {x^2} node[above,pos=1] {$x^2$};

或者如果您需要保存节点位置,请这样做:

\addplot[color=Firebrick2,samples=100,smooth,ultra thick] {x^2} node[pos=1] (endofplotsquare) {};
\node [above,color=Firebrick2] at (endofplotsquare) {$x^2$};

输出:

输出

解决方案:

\documentclass{article}
\usepackage[x11names]{xcolor}
\usepackage{tikz}
\usepackage{pgfplots}
\pgfplotsset{compat=newest}

\begin{document}

\begin{tikzpicture}
  \begin{axis}[
    grid = major,
    clip = true,
    clip mode=individual,
    restrict y to domain=-10:10,
    axis x line = middle,
    axis y line = middle,
    xlabel={$x$},
    xlabel style={at=(current axis.right of origin), anchor=west},
    ylabel={$y$},
    ylabel style={at=(current axis.above origin), anchor=south},
    domain = 0.01:5,
    xmin = 0,
    xmax = 5,
    enlarge y limits={rel=0.13},
    enlarge x limits={rel=0.07},
    ymin = -10,
    ymax = 10,
    after end axis/.code={\path (axis cs:0,0) node [anchor=north west,yshift=-0.075cm] {0} node [anchor=south east,xshift=-0.075cm] {0};},
  ]

    \addplot[color=Firebrick2,samples=100,smooth,ultra thick] {x^2} node[above,pos=1] {$x^2$};

    \addplot[color=DodgerBlue2,samples=100,smooth,ultra thick] {exp(x)} node[above,pos=1] {$\exp(x)$};

    \addplot[color=Gold3,samples=1000,smooth,ultra thick,unbounded coords=jump,no markers] {ln(x)} node[above,pos=1] {$\ln(x)$};

    %% Alternative %%

    % \addplot[color=Firebrick2,samples=100,smooth,ultra thick] {x^2} node[pos=1] (endofplotsquare) {};
    % \node [above,color=Firebrick2] at (endofplotsquare) {$x^2$};

    % \addplot[color=DodgerBlue2,samples=100,smooth,ultra thick] {exp(x)} node[pos=1] (endofplotsquare) {};
    % \node [above,color=DodgerBlue2] at (endofplotsquare) {$\exp(x)$};

    % \addplot[color=Gold3,samples=1000,smooth,ultra thick,unbounded coords=jump,no markers] {ln(x)} node[pos=1] (endofplotsquare) {};
    % \node [above,color=Gold3] at (endofplotsquare) {$\exp(x)$};
  \end{axis}
\end{tikzpicture}

\end{document}

答案2

这有点手动,你必须做一些反复试验,但它是有效的。你可以使用[pos=<fraction>]类似

node[pos=0.54](endofplotsquare){} ;

代码:

\documentclass{article}
\usepackage[x11names]{xcolor}
\usepackage{tikz}
\usepackage{pgfplots}
\begin{document}



\begin{tikzpicture}[domain=0.01:5]
\begin{axis}
[grid = major,
clip = true,
clip mode=individual,
axis x line = middle,
axis y line = middle,
xlabel={$x$},
xlabel style={at=(current axis.right of origin), anchor=west},
ylabel={$y$},
ylabel style={at=(current axis.above origin), anchor=south},
domain = 0.01:5,
xmin = 0,
xmax = 5,
enlarge y limits={rel=0.13},
enlarge x limits={rel=0.07},
ymin = -10,
ymax = 10,
after end axis/.code={\path (axis cs:0,0) node [anchor=north west,yshift=-0.075cm] {0} node [anchor=south east,xshift=-0.075cm] {0};}]

\addplot[color=Firebrick2, samples=100, smooth, ultra thick] {x^2}
node[pos=0.54](endofplotsquare){} ;
\node [right, color=Firebrick2] at (endofplotsquare) {$x^2$};

\addplot[color=DodgerBlue2, samples=100, smooth, ultra thick] {exp(x)};

\addplot[color=Gold3, samples=1000, smooth, ultra thick, unbounded coords=jump,  no markers] {ln(x)};
\end{axis}

\end{tikzpicture}

\end{document}

在此处输入图片描述

相关内容