외부 범례를 사용하여 플롯을 중앙에 배치하는 방법

외부 범례를 사용하여 플롯을 중앙에 배치하는 방법

외부 범례가 있는 플롯을 중앙에 배치하고 싶지만 전체 그림이 중앙에 배치됩니다(범례가 있는 플롯). 플롯의 위치는 외부 범례와 같아야 합니다.

\documentclass{article}
\usepackage[utf8]{inputenc}
\usepackage{pgfplots}
\usepackage{tikz}

\begin{document}

\begin{figure}[h]
\centering
  \begin{tikzpicture}
  \begin{semilogxaxis}[
  xmin=0.01,xmax=100,
  ymin=0,ymax=5,
  width=8cm,
  legend style={at={(1.03,0.5)},anchor=west,draw=none},
  legend cell align={left},
  grid]
  \addplot[red,domain=0.01:100,samples=400]{(2)/(1+x^2)};
  \addplot[blue,domain=0.01:100,samples=400]{(1+x^2)/(1-x^2+x^4)};
  \legend{$Q=0.5$,$Q=1$}
  \end{semilogxaxis}
  \end{tikzpicture}
\caption{Lorem Ipsum}
\end{figure}

\end{document}

다음과 같습니다: 여기에 이미지 설명을 입력하세요

그러나 다음과 같아야 합니다(물론 외부 범례가 추가됨). 여기에 이미지 설명을 입력하세요

답변1

원하는 것을 달성하는 가장 간단한 방법은 단순히 옵션을 추가하는 것 overlay입니다 legend style. 이는 경계 상자 결정 시 범례가 고려되는 것을 방지합니다.

% used PGFPlots v1.16
\documentclass{article}
\usepackage{pgfplots}
\begin{document}
\begin{figure}[h]
    \centering
    \begin{tikzpicture}
        \begin{semilogxaxis}[
            xmin=0.01,xmax=100,
            ymin=0,ymax=5,
            width=8cm,
            legend style={
                overlay,        % <-- added
                at={(1.03,0.5)},
                anchor=west,
                draw=none,
            },
            legend cell align={left},
            grid,
            domain=0.01:100,samples=400,
        ]
            \addplot [red]  {(2)/(1+x^2)};
            \addplot [blue] {(1+x^2)/(1-x^2+x^4)};
            \legend{$Q=0.5$,$Q=1$}
        \end{semilogxaxis}
    \end{tikzpicture}
    \caption{Lorem Ipsum}
\end{figure}
\end{document}

위 코드의 결과를 보여주는 이미지

답변2

다음을 찾고 계십니까?

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

(빨간색 선은 텍스트 테두리를 나타냅니다)

tikzpicture이를 위해서는 다음 옵션 만 추가하면 됩니다 trim axis left, trim axis right.

\documentclass{article}
\usepackage{pgfplots}
\pgfplotsset{compat=1.16}

%---------------- show page layout. don't use in a real document!
\usepackage{showframe}
\renewcommand\ShowFrameLinethickness{0.15pt}
\renewcommand*\ShowFrameColor{\color{red}}
%---------------------------------------------------------------%

\begin{document}
    \begin{figure}[ht]
\centering
  \begin{tikzpicture}[trim axis left, trim axis right] % <-------
  \begin{semilogxaxis}[
  xmin=0.01,xmax=100,
  ymin=0,ymax=5,
  width=8cm,
  legend style={at={(1.03,0.5)},anchor=west,draw=none},
  legend cell align={left},
  grid]
  \addplot[red,domain=0.01:100,samples=400]{(2)/(1+x^2)};
  \addplot[blue,domain=0.01:100,samples=400]{(1+x^2)/(1-x^2+x^4)};
  \legend{$Q=0.5{}$,$Q=1$}
  \end{semilogxaxis}
  \end{tikzpicture}
\caption{Lorem Ipsum}
    \end{figure}
\end{document}

관련 정보