tikz 图例中的多列

tikz 图例中的多列

我有一个简单的任务,到目前为止我找不到解决方案。我有一个包含 3 个图例条目的图,其中 2 个是短图例,1 个是长图例。我可以使用它legend columns=2,来获取两列图例,但它看起来像这样:

aaaa ________________ bbbb

cccccccccccccccccccccc

我喜欢的是:

aaaa ________ bbbb

cccccccccccccccccccccccccccc

(我将“_”用作空格)

那么,图例中使用的表格多列就类似这样。您知道如何实现吗?

谢谢,阿克塞尔

添加于 12.04.21:

抱歉没有添加提到的最小示例:我准备如下:

\documentclass{article}
\usepackage[utf8]{inputenc}

\usepackage{graphicx}
\usepackage{xcolor}
\usepackage{tikz}
\usetikzlibrary{decorations, shapes, calc, backgrounds, shadings, arrows.meta, trees, positioning}
\usepackage{pgfplots}
\pgfplotsset{compat=newest}

\begin{document}

\begin{figure}
\centering
    \begin{tikzpicture}
        \begin{axis}[%
        width=.7\textwidth,
        scale only axis,
        clip = false,
        legend columns=2,
        legend style={  at={(axis cs:1.5,1.2)},
            /tikz/column 2/.style={
                column sep=5pt},
            anchor=south}, 
        ]
        \addlegendimage{area legend,fill=red, opacity = 0.5}
        \addlegendimage{area legend,fill=gray, opacity = 0.5}
        \addlegendimage{area legend,fill=green, opacity = 0.5}
    
        \addplot[area legend, draw=black, fill=gray, fill opacity=0.5, forget plot]
        table[row sep=crcr] {%
        x   y\\
        1 0\\
        2 0\\
        2 1\\
        1 1\\
        }--cycle;
        \node[align=center]
        at (axis cs:1.5,0.5) {a};
        
        \addplot[area legend, draw=black, fill=red, fill opacity=0.5, forget plot]
        table[row sep=crcr] {%
        x   y\\
        0 0\\
        1 0\\
        1 1\\
        0 1\\
        }--cycle;
        \node[align=center]
        at (axis cs:0.5,0.5) {b};
        \addplot[area legend, draw=black, fill=green, fill opacity=0.5, forget plot]
        table[row sep=crcr] {%
        x   y\\
        2 0\\
        3 0\\
        3 1\\
        2 1\\
        }--cycle;
        \node[align=center]
        at (axis cs:2.5,0.5) {c};
        \legend{short text 1, short text 2, looooooooooooooooooooooooooooooooooooooooog text};  
        \end{axis}
    \end{tikzpicture}
    \caption{Caption}
    \label{fig:my_label}
\end{figure}
\end{document}

答案1

我看了你的例子,在这种情况下,我亲自用matrix中的选项构建了自己的图例\usetikzlibrary。如上所述这里对于较长的条目,您可以使用nodes={text width=width("the longest legend entry")}来设置节点(在本例中为图例)的宽度。对于条目 1 和 2 之间的空间,我添加了\hspace{length},您可以根据需要进行修改。

在此处输入图片描述

\documentclass[border=5pt]{standalone}

\usepackage{pgfplots}
\pgfplotsset{compat=newest}
\usetikzlibrary{matrix} % <- added for self made legend

\begin{document}
    
    \begin{tikzpicture}
        \begin{axis}[%
            name=plot,
            width=.7\textwidth,
            scale only axis,
            clip = false,
            legend columns=2,
            legend style={  at={(axis cs:1.5,1.2)},
                /tikz/column 2/.style={
                    column sep=5pt},
                anchor=south}, 
            ]
                                
            \addplot[area legend, draw=black, fill=gray, fill opacity=0.5, forget plot]
            table[row sep=crcr] {%
                x   y\\
                1 0\\
                2 0\\
                2 1\\
                1 1\\
            }--cycle;
            \node[align=center]
            at (axis cs:1.5,0.5) {a};
            
            \label{plot:a}
            
            \addplot[area legend, draw=black, fill=red, fill opacity=0.5, forget plot]
            table[row sep=crcr] {%
                x   y\\
                0 0\\
                1 0\\
                1 1\\
                0 1\\
            }--cycle;
            \node[align=center]
            at (axis cs:0.5,0.5) {b};
            
            \label{plot:b}
            
            \addplot[area legend, draw=black, fill=green, fill opacity=0.5, forget plot]
            table[row sep=crcr] {%
                x   y\\
                2 0\\
                3 0\\
                3 1\\
                2 1\\
            }--cycle;
            \node[align=center]
            at (axis cs:2.5,0.5) {c};
            
            \label{plot:c}
        
        \coordinate (legend) at (axis cs:1.5,1.2);
        
        \end{axis}
        
        \matrix[
        draw,
        matrix of nodes,
        anchor=south,
        font=\footnotesize,
        nodes={text width=width("looooooooooooooooooooooooooooooooooooooooog text")},
        ] at (legend) {
            \ref{plot:a} short text 1 \hspace{1cm} \ref{plot:b}  short text 2 \\
            \ref{plot:c} looooooooooooooooooooooooooooooooooooooooog text \\
        };

    \end{tikzpicture}
\end{document}

相关内容