我正在尝试将 PGFPlot 的图例移到右上角,但使用相对定位而at={(current axis.above of north east)}
不是手动指定位置at={(1, 1.02)}
。这可能吗?
在 Tikz 文档中,有类似的内容legend=north outside
,但这种语法似乎并不存在于 PGFPlots 中。
这是一个最小的工作示例:
\documentclass{standalone}
\usepackage{pgfplots}
\usepackage{tikz}
\usetikzlibrary{calc}
\usetikzlibrary{positioning}
\pgfplotsset{
compat=newest,
every axis legend/.append style = {
anchor=south east,
at={(1,1.02)},
draw=none
}
}
\begin{document}
\begin{tikzpicture}
\begin{axis}[grid=major, enlarge x limits=false]
\addplot[domain=-2:2, color=red, samples=50, smooth]{sin(pi*deg(x))};
\addlegendentry{$\sin(x)$};
\end{axis}
\end{tikzpicture}
\end{document}
答案1
尝试
anchor=south east,
at= {(current axis.north east)}
完整代码:
\documentclass{standalone}
\usepackage{pgfplots}
\usepackage{tikz}
\usetikzlibrary{calc}
\usetikzlibrary{positioning}
\pgfplotsset{
compat=1.12,
}
\begin{document}
\begin{tikzpicture}
\begin{axis}[grid=major, enlarge x limits=false,
legend style={
anchor=south east,
%cells={anchor=east},
draw=none,
at= {(current axis.north east)},
}]
\addplot[domain=-2:2, color=red, samples=50, smooth]{sin(pi*deg(x))};
\addlegendentry{$\sin(x)$};
\end{axis}
\end{tikzpicture}
\end{document}