pgfplots 图例中的换行

pgfplots 图例中的换行

我尝试用图例中的长文本来构思情节。

\documentclass{report}
\usepackage{pgfplots}
\pgfplotsset{
every axis legend/.append style={at={(0.5,-0.13)},anchor=north,legend cell align=left},
tick label style={font=\tiny\scriptsize},
legend style={font=\scriptsize},
axis lines=left,
legend style={draw=none},
}
\begin{document}
\begin{figure} [H]
\centering
\begin{tikzpicture}
\begin{axis} [
xlabel={x},
ylabel={y},
height=10cm,
width=17cm,
legend columns=2,
]
\addplot[color=red, mark=*] coordinates {
 (-3.79489, -0.794084) 
 (-3.54497, -0.919739) 
 (-3.51697, -0.93037) 
 (-3.3082, -0.986154) 
 (-0.165787, 0.986289) 
 (0.92704, 0.600204) 
};
\addlegendentry{Long long  long  long  long  long  long  long  long  long  long long  long  long  long  long  long  long  long  long  long  long  long  long  long  long  long  long  long  long  long text};

\addplot[color=blue, mark=*] coordinates {
 (-3.79489, 0.607808) 
 (-3.54497, 0.39253) 
 (-3.51697, 0.366622) 
 (-3.3082, 0.165834) 
 (-0.165787, -0.165029) 
 (0.92704, 0.799847) 
};
\addlegendentry{Plot 1};

\addplot[color=green, mark=*] coordinates {
 (-2.79489, 1.607808) 
 (-2.54497, 1.39253) 
 (-2.51697, 1.366622) 
 (-2.3082, 1.165834) 
 (-2.165787, -1.165029) 
 (1.92704, 1.799847) 
};

\addlegendentry{Plot 3};
\end{axis}
\end{tikzpicture}
\caption{Chart}
\end{figure}

\end{document}

但我看到:

在此处输入图片描述

如果我使用,legend style={text width=5.5cm},我会看到:

在此处输入图片描述

可以将图例中的文字换行吗?

答案1

图例是一个node,您可以指定minimum heighttext width等:

 \documentclass[border=10pt]{standalone}
\usepackage{pgfplots}
\pgfplotsset{
    compat=1.9,
    every axis legend/.append style={at={(0.5,-0.13)},anchor=north,legend cell align=left},
    tick label style={font=\tiny\scriptsize},
    legend style={draw=none,
              font=\scriptsize,
              text width=2in,           %% adjust
              minimum height=0.5in,     %% adjust
              %anchor=center,
              %cells={anchor=west},
            },
    %axis lines=left,
}
\begin{document}
\begin{tikzpicture}
\begin{axis} [
xlabel={x},
ylabel={y},
height=10cm,
width=11cm,
legend columns=2,
]
\addplot[color=red, mark=*] coordinates {
 (-3.79489, -0.794084)
 (-3.54497, -0.919739)
 (-3.51697, -0.93037)
 (-3.3082, -0.986154)
 (-0.165787, 0.986289)
 (0.92704, 0.600204)
};
\addlegendentry{Long long  long  long  long  long  long  long  long  long  long long  long  long  long  long  long  long  long  long  long  long  long  long  long  long  long  long  long  long  long text};

\addplot[color=blue, mark=*] coordinates {
 (-3.79489, 0.607808)
 (-3.54497, 0.39253)
 (-3.51697, 0.366622)
 (-3.3082, 0.165834)
 (-0.165787, -0.165029)
 (0.92704, 0.799847)
};
\addlegendentry{Plot 1};

\addplot[color=green, mark=*] coordinates {
 (-2.79489, 1.607808)
 (-2.54497, 1.39253)
 (-2.51697, 1.366622)
 (-2.3082, 1.165834)
 (-2.165787, -1.165029)
 (1.92704, 1.799847)
};

\addlegendentry{Plot 3};
\end{axis}
\end{tikzpicture}

\end{document}

在此处输入图片描述

答案2

谢谢!您的评论有助于找到解决方案。我可以[minimum height=0.9in]为每个标签使用:\addlegendentry[minimum height=0.9in]{111}

\documentclass{report}
\usepackage{pgfplots}
\pgfplotsset{
every axis legend/.append style={at={(0.5,-0.13)},anchor=north,legend cell align=left},
tick label style={font=\tiny\scriptsize},
legend style={font=\scriptsize},
axis lines=left,
legend style={draw=none},
}
\begin{document}
\begin{figure} [H]
\centering
\begin{tikzpicture}
\begin{axis} [
xlabel={x},
ylabel={y},
height=10cm,
width=17cm,
legend columns=2,
legend style={text width=5.5cm},
]
\addplot[color=red, mark=*] coordinates {
 (-3.79489, -0.794084) 
 (-3.54497, -0.919739) 
 (-3.51697, -0.93037) 
 (-3.3082, -0.986154) 
 (-0.165787, 0.986289) 
 (0.92704, 0.600204) 
};
\addlegendentry[minimum height=0.9in]{
Long long  long  long  long  long  long  long  long  long  long long  long  long  long  long  long  long  long  long  long  long  long  long  long  long  long  long  long  long  long text};

\addplot[color=blue, mark=*] coordinates {
 (-3.79489, 0.607808) 
 (-3.54497, 0.39253) 
 (-3.51697, 0.366622) 
 (-3.3082, 0.165834) 
 (-0.165787, -0.165029) 
 (0.92704, 0.799847) 
};
\addlegendentry{Plot 1};

\addplot[color=yellow, mark=*] coordinates {
 (-3., -0.607808) 
 (-3.97, 0.39253) 
 (-3.1697, 0.366622) 
 (-3.082, 0.165834) 
 (-0.65787, -0.165029) 
 (0.2704, 0.799847) 
};
\addlegendentry{Plot 1 4 4 6465 46 46 46};

\addplot[color=black, mark=*] coordinates {
 (-3.9489, 0.07808) 
 (-3.4497, 0.9253) 
 (-3.1697, 0.66622) 
 (-3.082, 0.65834) 
 (-0.65787, -0.65029) 
 (0.2704, 0.99847) 
};
\addlegendentry{Plot new test};

\addplot[color=green, mark=*] coordinates {
 (-3.9489, 0.47808) 
 (-3.4497, -0.3253) 
 (-3.1697, 0.36622) 
 (-3.082, 0.15834) 
 (-0.65787, -0.05029) 
 (0.2704, 0.09847) 
};
\addlegendentry{Plot new test};

\addlegendentry{Plot 3};
\end{axis}
\end{tikzpicture}
\caption{Chart}
\end{figure}

\end{document}

在此处输入图片描述

相关内容