pgfplot 外部的多行图例

pgfplot 外部的多行图例

我正在构建一个图,但pgfplots图表框内没有适合放置图例的位置,所以我试图将图例放在外面。下面是代码……如果没有所有数据文件,它显然无法构建,但您可以在下面看到结果

\begin{figure}
    \centering
    \begin{tikzpicture}
        \begin{axis}[
            legend columns=-1,
            legend entries={{\tiny Random},{\tiny +Cost},{\tiny +FTE},{\tiny ++Cost},{\tiny ++FTE},{\tiny ++Resources}, {\tiny ++Cost$_{tri}$}, {\tiny ++FTE$_{tri}$}, {\tiny ++Resources$_{tri}$}, {\tiny ++Cost$_{LN}$}, {\tiny ++FTE$_{expo}$}, {\tiny ++Resources$_{expo}$}},
                legend to name=DollarsLegend,
                no markers,
                xlabel = Simulation Day,
                ylabel = Dollars Committed,
                                xmin=0,
                                xmax=260,
                width=\textwidth,
                y tick label style={
                  /pgf/number format/.cd,
                  fixed,
                  fixed zerofill,
                  precision=0,
                  /tikz/.cd
                },
                x tick label style={
                  /pgf/number format/.cd,
                  fixed,
                  fixed zerofill,
                  precision=0,
                 /tikz/.cd
                },
                scaled ticks=false,
                yticklabel={${\$\pgfmathprintnumber{\tick}}$},
                ]
            \addplot table[line join=round,col sep=comma, y=Random, x=Day]{DollarCommitment.csv};
            \addlegendentry{{\scriptsize Random}}
            \addplot table[line join=round,col sep=comma, y=PlusCost, x=Day]{DollarCommitment.csv};
            \addlegendentry{{\scriptsize +Cost}}
            \addplot table[line join=round,col sep=comma, y=PlusFTE, x=Day]{DollarCommitment.csv};
            \addlegendentry{{\scriptsize +FTE}}
            \addplot table[line join=round,col sep=comma, y=PlusPlusCost, x=Day]{DollarCommitment.csv};
            \addlegendentry{{\scriptsize ++Cost}}
            \addplot table[line join=round,col sep=comma, y=PlusPlusFTE, x=Day]{DollarCommitment.csv};
            \addlegendentry{{\scriptsize ++FTE}}
            \addplot table[line join=round,col sep=comma, y=PlusPlusResources, x=Day]{DollarCommitment.csv};
            \addlegendentry{{\scriptsize ++Resources}}
            \addplot table[line join=round,col sep=comma, y=PlusPlusCostTri, x=Day]{DollarCommitment.csv};
            \addlegendentry{{\scriptsize ++Cost$_{tri}$}}
            \addplot table[line join=round,col sep=comma, y=PlusPlusFTETri, x=Day]{DollarCommitment.csv};
            \addlegendentry{{\scriptsize ++FTE$_{tri}$}}
            \addplot table[line join=round,col sep=comma, y=PlusPlusResourcesTri, x=Day]{DollarCommitment.csv};
            \addlegendentry{{\scriptsize ++Resources$_{tri}$}}
            \addplot table[line join=round,col sep=comma, y=PlusPlusCostLN, x=Day]{DollarCommitment.csv};
            \addlegendentry{{\scriptsize ++Cost$_{LN}$}}
            \addplot table[line join=round,col sep=comma, y=PlusPlusFTEExpo, x=Day]{DollarCommitment.csv};
            \addlegendentry{{\scriptsize ++FTE$_{expo}$}}
            \addplot table[line join=round,col sep=comma, y=PlusPlusResourcesExpo, x=Day]{DollarCommitment.csv};
            \addlegendentry{{\scriptsize ++Resources$_{expo}$}}
        \end{axis}
    \end{tikzpicture}
        \ref{DollarsLegend}
    \caption{Dollar Commitment versus Time}
    \label{DollarCommitment}
\end{figure}

这是结果图

剧情截图

我想要legend跨越两条(如果需要的话更多)线...我该如何实现呢?

答案1

legend columns=-1,的代码中将图例保持在一行中。将其更改为,例如,legend columns=4,这样就有四列。根据需要调整此值。

\documentclass{article}

\usepackage{pgfplots}
\usepgfplotslibrary{groupplots}
\pgfplotsset{compat=1.12}
\begin{document}
   \begin{figure}
    \centering
    \begin{tikzpicture}
        \begin{axis}[
            legend columns=4,    %%<------ changed
            legend entries={{\tiny Random},{\tiny +Cost},{\tiny +FTE},{\tiny ++Cost},{\tiny ++FTE},{\tiny ++Resources}, {\tiny ++Cost$_{tri}$}, {\tiny ++FTE$_{tri}$}, {\tiny ++Resources$_{tri}$\\}, {\tiny ++Cost$_{LN}$}, {\tiny ++FTE$_{expo}$}, {\tiny ++Resources$_{expo}$}},
                legend to name=DollarsLegend,
                no markers,
                xlabel = Simulation Day,
                ylabel = Dollars Committed,
                                xmin=0,
                                xmax=260,
                width=0.9\textwidth,        %%<------ changed
                y tick label style={
                  /pgf/number format/.cd,
                  fixed,
                  fixed zerofill,
                  precision=0,
                  /tikz/.cd
                },
                x tick label style={
                  /pgf/number format/.cd,
                  fixed,
                  fixed zerofill,
                  precision=0,
                 /tikz/.cd
                },
                scaled ticks=false,
                yticklabel={${\$\pgfmathprintnumber{\tick}}$},
                ]
            \addplot table[line join=round,col sep=comma, y=Random, x=Day]{DollarCommitment.csv};
            \addlegendentry{{\scriptsize Random}}
            \addplot table[line join=round,col sep=comma, y=PlusCost, x=Day]{DollarCommitment.csv};
            \addlegendentry{{\scriptsize +Cost}}
            \addplot table[line join=round,col sep=comma, y=PlusFTE, x=Day]{DollarCommitment.csv};
            \addlegendentry{{\scriptsize +FTE}}
            \addplot table[line join=round,col sep=comma, y=PlusPlusCost, x=Day]{DollarCommitment.csv};
            \addlegendentry{{\scriptsize ++Cost}}
            \addplot table[line join=round,col sep=comma, y=PlusPlusFTE, x=Day]{DollarCommitment.csv};
            \addlegendentry{{\scriptsize ++FTE}}
            \addplot table[line join=round,col sep=comma, y=PlusPlusResources, x=Day]{DollarCommitment.csv};
            \addlegendentry{{\scriptsize ++Resources}}
            \addplot table[line join=round,col sep=comma, y=PlusPlusCostTri, x=Day]{DollarCommitment.csv};
            \addlegendentry{{\scriptsize ++Cost$_{tri}$}}
            \addplot table[line join=round,col sep=comma, y=PlusPlusFTETri, x=Day]{DollarCommitment.csv};
            \addlegendentry{{\scriptsize ++FTE$_{tri}$}}
            \addplot table[line join=round,col sep=comma, y=PlusPlusResourcesTri, x=Day]{DollarCommitment.csv};
            \addlegendentry{{\scriptsize ++Resources$_{tri}$}}
            \addplot table[line join=round,col sep=comma, y=PlusPlusCostLN, x=Day]{DollarCommitment.csv};
            \addlegendentry{{\scriptsize ++Cost$_{LN}$}}
            \addplot table[line join=round,col sep=comma, y=PlusPlusFTEExpo, x=Day]{DollarCommitment.csv};
            \addlegendentry{{\scriptsize ++FTE$_{expo}$}}
            \addplot table[line join=round,col sep=comma, y=PlusPlusResourcesExpo, x=Day]{DollarCommitment.csv};
            \addlegendentry{{\scriptsize ++Resources$_{expo}$}}
        \end{axis}
    \end{tikzpicture}
\ref{DollarsLegend}
    \caption{Dollar Commitment versus Time}
    \label{DollarCommitment}
\end{figure}
\end{document}

在此处输入图片描述

相关内容