PGFplot 범례를 축과 정확히 일치하도록 상단 정렬하는 방법

PGFplot 범례를 축과 정확히 일치하도록 상단 정렬하는 방법

내 목표는 범례 상자를 플롯과 상단 정렬하여 상단 축 프레임과 정확히 같은 높이가 되도록 하는 것입니다.

다음 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}

여기에 이미지 설명을 입력하세요

관련 정보