
いくつかのプロットにラベルを付けようとしているのですが、どうもうまくいきません。次のようなものを作成しようとしています。
texampleのウェブサイトで見つかりましたここ上図のように、各プロットに対応する関数にラベルを付けたいと思います。
私が持っているのは次のものです:
\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}
これにより、次のようになります。
スタイル上の理由から、axis 環境に留まることを希望していました。
答え1
node
曲線に沿って を配置するソリューションを次に示します。必要に応じて、(ドメインの始まり) と(ドメインの終わり)pos
の間のキーを変更します。アンカー ( 、など) を操作することもできます。0
1
east
west
% 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}