
Я пытаюсь маркировать некоторые графики, но не могу понять, как это сделать. Я пытаюсь получить что-то вроде этого:
Найдено на сайте 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}
который производит:
Я надеялся остаться в среде 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}