我想將帶有外部圖例的圖居中,但整個圖形都居中(帶有圖例的圖)。劇情的位置應該是像外界傳說的那樣不存在。
\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}