標籤圖 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}

在此輸入影像描述

相關內容