2

I got a legend with two rows and I would like to have a different alignment on each row. Is it possible ?

In my current code, the legend entries on the second line are much longer than ones on the first line. This results in extra space between 1st line legend entries and I would like to avoid that. Here is my code :

\documentclass{article}
\usepackage{pgfplots}
\pgfplotsset{compat=newest}

\begin{document}
\begin{figure}
\begin{tikzpicture}
\begin{semilogyaxis}[xmin=0, xmax=600, ymin=1e-1, ymax=1e7, axis x line*=bottom, axis y line*=left, xlabel={X}, ylabel={Y},width=0.9\textwidth, height=0.5\textwidth, legend columns=3, legend style={cells={anchor=west}, at={(1,0.3)}, anchor=east}]

\addplot [color=red, domain=0:600, samples=1000, thick, mark=none] {(1+4*x^2) };
\addlegendentry{A}
\addplot [color=blue, domain=0:600, samples=1000, thick, mark=none] {1+2*x };
\addlegendentry{B}
\addplot [color=green!80!black, domain=0:600, samples=1000, thick, mark=none] {1.};
\addlegendentry{C}

\addlegendimage{black, thick}
\addlegendentry{Numerical}
\addlegendimage{black, ultra thick, loosely dashed}
\addlegendentry{Analytical}

\addplot[color=red, domain=0:600, loosely dashed, samples=1000, ultra thick] {(1+4*x^2) };
\addplot[color=blue, domain=0:600, loosely dashed, samples=1000, ultra thick] {1+2*x };
\addplot[color=green!80!black, domain=0:600, loosely dashed, samples=1000, ultra thick] {1.};

\end{semilogyaxis}
\end{tikzpicture}
\end{figure}
\end{document}

Thanks !

  • The legend is set as a matrix of nodes so changing the number of columns from line to line is not possible. legend columns=-1 would put all entries in one row, which might be more appropriate for you. – Andrew Swann Jul 08 '14 at 08:09
  • Thanks ! I did not know it was a matrix of nodes. And yes maybe only 1 row can do it but it was not the question :) – StormRider Jul 08 '14 at 08:22

1 Answers1

1

I found a dirty way to do it ... it works but I think it is not the best way :) Does anyone have another idea ?

\documentclass{article}
\usepackage{pgfplots}
\pgfplotsset{compat=newest}

\begin{document}
\begin{figure}
\begin{tikzpicture}
\begin{semilogyaxis}[xmin=0, xmax=600, ymin=1e-1, ymax=1e7, axis x line*=bottom, axis y line*=left, xlabel={X}, ylabel={Y},width=0.9\textwidth, height=0.5\textwidth, legend columns=3, legend style={fill=none, draw=none, cells={anchor=west}, at={(0.74,0.3)}, anchor=south}
]

\addplot [color=red, domain=0:600, samples=10, thick, mark=none] {(1+4*x^2) };
\addlegendentry{A}
\addplot [color=blue, domain=0:600, samples=10, thick, mark=none] {1+2*x };
\addlegendentry{B}
\addplot [color=green!80!black, domain=0:600, samples=10, thick, mark=none] {1.};
\addlegendentry{C}

\addplot[color=red, domain=0:600, loosely dashed, samples=10, ultra thick] {(1+4*x^2) };
\addplot[color=blue, domain=0:600, loosely dashed, samples=10, ultra thick] {1+2*x };
\addplot[color=green!80!black, domain=0:600, loosely dashed, samples=10, ultra thick] {1.};

\end{semilogyaxis}
\begin{semilogyaxis}[xmin=0, xmax=600, ymin=1e-1, ymax=1e7, axis x line*=bottom, axis y line*=left, xlabel={X}, ylabel={Y},width=0.9\textwidth, height=0.5\textwidth, legend columns=2, legend style={fill=none, draw=none, cells={anchor=west}, at={(0.74,0.3)}, anchor=north}
]

\addlegendimage{black, ultra thick, loosely dashed}
\addlegendentry{Analytical}
\addlegendimage{black, thick}
\addlegendentry{Numerical}

\draw[black, solid, thick] (axis description cs:0.47,0.18) rectangle (axis description cs:1,0.42);

\end{semilogyaxis}
\end{tikzpicture}
\end{figure}
\end{document}