
我的目標是將圖例框與繪圖頂部對齊,使其與頂部軸框架完全齊平。
下面的 MWE 乍看之下工作正常,但仔細觀察時,會發現有一個小的垂直偏移(見圖)。一旦看見,就不可能看不見。
\documentclass[tikz,margin=10pt]{standalone}
\usepackage{pgfplots}
\begin{document}
\begin{tikzpicture}
\begin{axis}[
legend style={
legend pos=outer north east
}
]
\addplot[color=gray!50,mark=x] coordinates {
(1,1)
};
\addlegendentry{Stuff}
\addplot[color=gray,mark=x] coordinates {
(2,2)
};
\addlegendentry{Other stuff}
\end{axis}
\end{tikzpicture}
\end{document}
我從其他答案中知道圖例是 TikZ 矩陣。我知道可以使用座標(軸 cs 或其他)來定位圖例。但即使使用如下所示的硬編碼定位,邊框也不會完全對齊。
\begin{axis}[
legend style={
at={(axis cs:2.6,2.1)}
},
ymax=2.1
]
如果相關的話,我正在使用 TeX Live 2019 安裝。
答案1
歡迎來到 TeX.SE。添加outer sep = 0pt
到您的圖例樣式可能是一個合適的解決方案。
outer sep=0pt
我添加了一些程式碼,將圖例與畫布邊界齊平,並建議在添加到樣式時畫布頂部和圖例對齊。
\documentclass[tikz,margin=10pt]{standalone}
\usepackage{pgfplots}
\begin{document}
\begin{tikzpicture}
\begin{axis}[
legend style={
outer sep=0pt,
legend pos=outer north east
}
% legend style={
% anchor=north west,
% outer sep=0pt,
% at= {(current axis.north east)},
%}
]
\addplot[color=gray!50,mark=x] coordinates {
(1,1)
};
\addlegendentry{Stuff}
\addplot[color=gray,mark=x] coordinates {
(2,2)
};
\addlegendentry{Other stuff}
\end{axis}
\end{tikzpicture}
\end{document}