我想将带有外部图例的图置于中心,但整个图形都处于中心位置(带有图例的图)。图的位置应该像外部图例不存在一样。
\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}