标签图 GnuPlot 和 tikz

标签图 GnuPlot 和 tikz

我正在尝试标记一些图,但似乎无法弄清楚。我试图得到一些看起来有点像这样的东西:

在此处输入图片描述

在 texanmple 网站上找到这里。我想标记每个图对应的函数,如上图所示。

这就是我所拥有的:

\documentclass[tikz]{standalone}

\usepackage{pgfplots}

\begin{document}
\begin{tikzpicture}[plot/.style={very thick,raw gnuplot,mark=none,black}]
    \begin{axis}[
    minor y tick num=2,
    minor x tick num=1,
    extra y ticks={0},
    extra x ticks={0},
    extra tick style={grid style={black},xticklabel=\empty},
    width=0.7\textwidth,
    ymin=-2, ymax=2,
    grid=both, y=1cm,
    axis y line=left,
    axis x line=bottom
    ]
    \addplot gnuplot [plot, samples=500, restrict y to domain=-10:10] {
        plot [-5:5] x**3 - x;
    };

    \addplot gnuplot [plot, blue, samples=500, restrict y to domain=-10:10] {
        plot [-5:5] (x-3)**3 - (x-3);   
    };
    \addplot gnuplot [plot, red, samples=500, restrict y to domain=-10:10] {
        plot [-5:5] (x+3)**3 - (x+3);
    };
\end{axis}\end{tikzpicture}
\end{document}

生成结果:

在此处输入图片描述

由于样式原因,我希望留在轴环境中。

答案1

这是一个解决方案,将 放置node在曲线上;根据需要在(域的开始) 和(域的结束)pos之间改变键。您还可以使用锚点,例如、等。01eastwest

在此处输入图片描述

% arara: pdflatex: {shell: yes}
\documentclass[tikz]{standalone}

\usepackage{pgfplots}

\begin{document}
\begin{tikzpicture}[plot/.style={very thick,raw gnuplot,mark=none,black}]
    \begin{axis}[
    minor y tick num=2,
    minor x tick num=1,
    extra y ticks={0},
    extra x ticks={0},
    extra tick style={grid style={black},xticklabel=\empty},
    ymin=-2, ymax=2,
    grid=both, y=1cm,
    axis y line=left,
    axis x line=bottom
    ]
    \addplot gnuplot [plot, samples=500, restrict y to domain=-10:10] {
        plot [-2:2] x**3 - x;
        }node[pos=0.7,anchor=west]{$y=f(x)$};

    \addplot gnuplot [plot, blue, samples=500, restrict y to domain=-10:10] {
        plot [1:5] (x-3)**3 - (x-3);   
    }node[pos=0.3,anchor=west]{$y=g(x)$};
    \addplot gnuplot [plot, red, samples=500, restrict y to domain=-10:10] {
        plot [-5:-1] (x+3)**3 - (x+3);
    }node[pos=0.7,anchor=east]{$y=h(x)$};
\end{axis}\end{tikzpicture}
\end{document}

作为参考,这是一个没有gnuplot

% arara: pdflatex
\documentclass[tikz]{standalone}

\usepackage{pgfplots}

\begin{document}
\begin{tikzpicture}[plot/.style={very thick,mark=none,black,samples=100}]
    \begin{axis}[
    minor y tick num=2,
    minor x tick num=1,
    extra y ticks={0},
    extra x ticks={0},
    extra tick style={grid style={black},xticklabel=\empty},
    ymin=-2, ymax=2,
    grid=both, y=1cm,
    axis y line=left,
    axis x line=bottom
    ]
    \addplot [plot,domain=-2:2]{x^3 - x}node[pos=0.7,anchor=west]{$y=f(x)$};
    \addplot [plot,blue, domain=1:5] { (x-3)^3 - (x-3)}node[pos=0.3,anchor=west]{$y=g(x)$};
    \addplot [plot,red, domain=-5:-1] {(x+3)^3 - (x+3)}node[pos=0.7,anchor=east]{$y=h(x)$};
\end{axis}
\end{tikzpicture}
\end{document}

最后,如果您想要轴外的标签,请查看带有标记图的 PgfPlots 延伸到图形框之外

答案2

您可以按如下方式向 pgfplot 轴添加图例。这样够了吗?还是您想要图例文本的颜色?

\documentclass[tikz,border=10pt]{standalone}

\usepackage{pgfplots}

\begin{document}
    \begin{tikzpicture}[plot/.style={very thick,raw gnuplot,mark=none,black}]
    \begin{axis}[
    minor y tick num=2,
    minor x tick num=1,
    extra y ticks={0},
    extra x ticks={0},
    extra tick style={grid style={black},xticklabel=\empty},
    width=0.7\textwidth,
    ymin=-2, ymax=2,
    grid=both, y=1cm,
    axis y line=left,
    axis x line=bottom,
    legend style={at={(1.1,1)}, anchor={north west}},
    ]
    \addplot gnuplot [plot, samples=500, restrict y to domain=-10:10] {
        plot [-5:5] x**3 - x;
    };

    \addplot gnuplot [plot, blue, samples=500, restrict y to domain=-10:10] {
        plot [-5:5] (x-3)**3 - (x-3);   
    };
    \addplot gnuplot [plot, red, samples=500, restrict y to domain=-10:10] {
        plot [-5:5] (x+3)**3 - (x+3);
    };
    \legend{$ x^3 - x $,$ (x-3)^3 - (x-3) $,$ (x+3)^3 - (x+3) $}
    \end{axis}\end{tikzpicture}
\end{document}

在此处输入图片描述

相关内容