如何绘制没有周围矩形的水平(行)图例?

如何绘制没有周围矩形的水平(行)图例?

我发现以下代码可以手动绘制图例,但它会产生垂直(列)图例。如何水平绘制?我也不想要周围的矩形。

\documentclass{article}

\usepackage{pgfplots}

% argument #1: any options
\newenvironment{customlegend}[1][]{%
    \begingroup
    % inits/clears the lists (which might be populated from previous
    % axes):
    \csname pgfplots@init@cleared@structures\endcsname
    \pgfplotsset{#1}%
}{%
    % draws the legend:
    \csname pgfplots@createlegend\endcsname
    \endgroup
}%

% makes \addlegendimage available (typically only available within an
% axis environment):
\def\addlegendimage{\csname pgfplots@addlegendimage\endcsname}

\begin{document}

\thispagestyle{empty}

\begin{tikzpicture}
    \begin{customlegend}[legend entries={$a$,$e^x$,C,$d$}]
    \addlegendimage{red,fill=black!50!red,area legend}
    \addlegendimage{red,fill=black!50!red,sharp plot}
    \addlegendimage{red,fill=black!50!red,mark=*,sharp plot}
    \addlegendimage{red,fill=black!50!red,ybar,ybar legend}
    \end{customlegend}
\end{tikzpicture}

\end{document} 

输出:

在此处输入图片描述

答案1

最初,你可以使用legend style={draw=none, legend columns=-1},它将水平绘制图例,并且图例周围没有框。

draw=none部件工作正常,legend columns关键部分在样式上不再起作用legend style。但在外面可以工作。

因此:

\begin{customlegend}[
  legend columns=-1,
  legend style={
    draw=none,
    column sep=1ex,
  },
  legend entries={$a$,$e^x$,C,$d$}
]

\makeatletter我也用过\makeatother因此您可以将其用作@字母,而不用\csname。(请参阅\makeatletter 和 \makeatother 起什么作用?

代码

\documentclass[tikz]{standalone}

\usepackage{pgfplots}

% argument #1: any options
\makeatletter
\newenvironment{customlegend}[1][]{%
    \begingroup
    % inits/clears the lists (which might be populated from previous
    % axes):
    \pgfplots@init@cleared@structures
    \pgfplotsset{#1}%
}{%
    % draws the legend:
    \pgfplots@createlegend
    \endgroup
}%

% makes \addlegendimage available (typically only available within an
% axis environment):
\def\addlegendimage{\pgfplots@addlegendimage}
\makeatother
\begin{document}
\begin{tikzpicture}
    \begin{customlegend}[legend columns=-1,legend style={draw=none,column sep=1ex},legend entries={$a$,$e^x$,C,$d$}]
    \addlegendimage{red,fill=black!50!red,area legend}
    \addlegendimage{red,fill=black!50!red,sharp plot}
    \addlegendimage{red,fill=black!50!red,mark=*,sharp plot}
    \addlegendimage{red,fill=black!50!red,ybar,ybar legend}
    \end{customlegend}
\end{tikzpicture}
\end{document}

输出

在此处输入图片描述

相关内容