有时,我们的图例条目大小(长度)相差很大。当图例位于轴内时,会浪费空间;当图例位于轴外时,会占用很大的地块。例如,如果图例条目的长度大致有两种(短和长),如下图所示,那么使用一个具有自定义尺寸的图例框会很方便。
我的解决方法是仅创建单独的图例框:
\documentclass[convert]{standalone}
\usepackage{pgfplotstable}
\pgfplotsset{compat=1.16}
\begin{document}
\begin{tikzpicture}
\begin{axis}[legend pos=south west, domain=0:100, no marks]
\addplot+[orange] {0};
\addlegendentry{long legend entry indeed}
\addplot {-x + 1}; \label{a}
\addplot {-x + 4}; \label{b}
\addplot {-x + 9}; \label{c}
\addplot {-x + 1}; \label{d}
% Desired look of legend box
\draw (60,0) -- (80,0) -- (80, -20) -- (100,-20) -- (100, -30) -- (60, -30) -- (60, 0);
\end{axis} % The following is quite tedious
\begin{axis}[axis x line=none, axis y line=none, legend style={at={(axis cs:-5.63, -0.5)}, anchor=south west},]
\addplot[opacity=0, forget plot] {0};
\addlegendimage{/pgfplots/refstyle=a} \addlegendentry{$c_1$}
\addlegendimage{/pgfplots/refstyle=b} \addlegendentry{$c_2$}
\addlegendimage{/pgfplots/refstyle=c} \addlegendentry{$c_3$}
\addlegendimage{/pgfplots/refstyle=d} \addlegendentry{$c_4$}
\end{axis}
\end{tikzpicture}
\end{document}
请注意,如果使用单个图例框,它将覆盖图的很大一部分区域。这尽管诸如在全球范围内运作legend style
之类的选项,帖子看起来都是相关的。text depth
答案1
pgfplots 似乎没有将图例条目存储在我们可以轻松访问的节点中,因此最简单的选择可能是使用路径图片。不幸的是,这意味着我们需要手动添加两个维度。
\documentclass[convert]{standalone}
\usepackage{pgfplots}
\usepackage{pgfplotstable}
\pgfplotsset{compat=1.16}
\begin{document}
\begin{tikzpicture}
\begin{axis}[legend pos=south west,
legend cell align=left,
legend style={fill=none,draw=none,
path picture={\draw
([xshift=\pgflinewidth/2,yshift=-\pgflinewidth/2]path picture bounding box.north west)
-- ++ (3.5em,0cm) |-
([xshift=-\pgflinewidth/2,yshift=1.8em]path picture bounding box.south east) --
([xshift=-\pgflinewidth/2,yshift=\pgflinewidth/2]path picture bounding box.south east)
-| cycle;}
},
domain=0:100, no marks]
\addplot {-x + 1};
\addlegendentry{$c_1$}
\addplot {-x + 4};
\addlegendentry{$c_2$}
\addplot {-x + 9};
\addlegendentry{$c_3$}
\addplot {-x + 1};
\addlegendentry{$c_4$}
\addplot+[orange] {0};
\addlegendentry{long legend entry indeed}
% Desired look of legend box
\draw (60,0) -- (80,0) -- (80, -20) -- (100,-20) -- (100, -30) -- (60, -30) -- (60, 0);
\end{axis} % The following is quite tedious
\end{tikzpicture}
\end{document}
如果您删除所有path picture
内容并只保留,fill=none
您将获得一个没有边框的图例,我也是如此。