我在將圖例條目正確放置在 3x3 形狀的轉置圖例矩陣中時遇到問題,其中以下 MWE 的條目為空(跳過):
\documentclass{standalone}
\usepackage{pgfplots}
\pgfplotsset{compat=1.16}
\begin{document}
\begin{tikzpicture}
\begin{axis}[%
legend columns = 3,
transpose legend,
]
\addplot {-x+1};\addlegendentry{A}
\addplot {-x+2};\addlegendentry{B}
\addplot {-x+3};\addlegendentry{C}
\addplot {-x+4};\addlegendentry{D}
\addplot {-x+5};\addlegendentry{E}
\addlegendimage{empty legend}\addlegendentry{skip me}
\addplot {-x+7};\addlegendentry{F}
%\addlegendimage{empty legend}\addlegendentry{skip me}
%\addlegendimage{empty legend}\addlegendentry{skip me}
\end{axis}
\end{tikzpicture}
\end{document}
我得到的是:
但我期望的是:
A D F
B E
C
我發現取消註解最後兩empty legend
行可以獲得預期的結果:
\documentclass{standalone}
\usepackage{pgfplots}
\pgfplotsset{compat=1.16}
\begin{document}
\begin{tikzpicture}
\begin{axis}[%
legend columns = 3,
transpose legend,
]
\addplot {-x+1};\addlegendentry{A}
\addplot {-x+2};\addlegendentry{B}
\addplot {-x+3};\addlegendentry{C}
\addplot {-x+4};\addlegendentry{D}
\addplot {-x+5};\addlegendentry{E}
\addlegendimage{empty legend}\addlegendentry{skip me}
\addplot {-x+7};\addlegendentry{F}
\addlegendimage{empty legend}\addlegendentry{skip me} % <-- Uncommented
\addlegendimage{empty legend}\addlegendentry{skip me} % <-- Uncommented
\end{axis}
\end{tikzpicture}
\end{document}
這樣,我得到了預期的結果:
儘管我找到了最初問題的解決方案,但我想知道這是否是預期的行為! ?我在 中找不到任何相關內容PGFPLOTS 文檔。
答案1
如果這符合您的要求,請勾選左側的複選標記
\documentclass{standalone}
\usepackage{pgfplots}
\pgfplotsset{compat=1.16}
\begin{document}
\begin{tikzpicture}
\begin{axis}[%
legend columns = 3,
transpose legend,
]
\addplot {-x+1};\addlegendentry{A}
\addplot {-x+2};\addlegendentry{B}
\addplot {-x+3};\addlegendentry{C}
\addplot {-x+4};\addlegendentry{D}
\addplot {-x+5};\addlegendentry{E}
\addlegendimage{empty legend}\addlegendentry{}
\addplot {-x+7};\addlegendentry{F}
\addlegendimage{empty legend}\addlegendentry{} % <-- Uncommented
\addlegendimage{empty legend}\addlegendentry{} % <-- Uncommented
\end{axis}
\end{tikzpicture}
\end{document}
請記住,您還必須以這種方式指定圖例的錨點:
legend style={at={(0.03,0.5)},anchor=west}
錨點定義圖例框的哪個點將放置在您定義的座標處at={(<>,<>)}
。
如果您僅使用at={(<>,<>)}
插入的座標,則該座標是軸框的座標,其中該點(0,0)
是左下角和(1,1)
右上角。
如果您使用,則at={(axis cs:<>,<>)}
指定軸的實際座標,與您的繪圖相同。
例子
legend style={at={(axis cs:0.5,1)},anchor=south west}
或者
\begin{axis}[legend pos=north west]
或者
\begin{axis}[legend style={at={(0.5,-0.1)},anchor=north}]
或者
\begin{axis}
[%
axis lines = middle,
xlabel = $x$,
ylabel = {$f(x)$},
enlarge y limits=upper,
yticklabel style = {font=\tiny},
xticklabel style = {font=\tiny},
legend style={xshift=1.5cm},
thick,
]%
預設位置是north east
。