Cómo centrar una trama con leyendas exteriores

Cómo centrar una trama con leyendas exteriores

Me gustaría centrar una trama con leyendas exteriores, pero se centra toda la figura (trama con leyendas). La posición de la trama debe ser como si la leyenda exterior no estuviera allí.

\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}

Parece: ingrese la descripción de la imagen aquí

Pero debería verse así (por supuesto, con la leyenda exterior agregada): ingrese la descripción de la imagen aquí

Respuesta1

Supongo que la forma más sencilla de lograr lo que desea es simplemente agregar overlayopciones legend style. Esto evita que la leyenda se tenga en cuenta en la determinación del cuadro delimitador.

% 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}

imagen que muestra el resultado del código anterior

Respuesta2

¿Estás buscando lo siguiente?

ingrese la descripción de la imagen aquí

(las líneas rojas designan los bordes del texto)

Para esto solo necesitas agregar a tikzpicturela opción 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}

información relacionada