1

I want to enlarge the width of the legend box to the width of the box of the plot. The result should look like this:

enter image description here

I tried to adjust the width by using

legend style={minimum width=xx cm},

but this didn't work properly and I would have to figure out the width manually for every plot.

\documentclass{standalone}
\usepackage{tikz}
\usepackage{pgfplots}
\pgfplotsset{compat=1.12}
\begin{document}
\begin{tikzpicture}[]
\pgfplotsset{every axis legend/.append style={
    at={(0.5,1.0)},
    anchor=south}}
\begin{axis}[%
width=15.5cm,
height=6cm,
scaled x ticks=true,
scaled y ticks=false,
x tick label style={
    /pgf/number format/.cd,
    fixed zerofill,
    precision=2,
    use comma,
    1000 sep={},
    /tikz/.cd
},
xticklabel={\ifdim\tick pt=0pt 0 \else\pgfmathprintnumber{\tick}\fi},
y tick label style={
    /pgf/number format/.cd,
    fixed,
    fixed zerofill,
    precision=4,
    use comma,
    1000 sep={},
    /tikz/.cd
},
yticklabel={\ifdim\tick pt=0pt 0 \else\pgfmathprintnumber{\tick}\fi},
xlabel={x-axis},
ylabel={y-axis},
xmin=0,
xmax=1000,
xtick={0,250,500,750,1000},
xticklabels={0,25000,50000,75000,100000},
ymin=0.0012,
ymax=0.0015,
ytick={0.0012,0.0013,0.0014,0.0015},
grid style={line width=.1pt, draw=gray!50},
major grid style={line width=.2pt,draw=gray!80},
grid = both,
minor tick num=1,
legend columns=3,
]   
\addplot [color=black,line width=1.5pt, smooth] 
table[row sep=crcr]{%
    3.60    0.0012304 \\
    13.20   0.0013751 \\
    50.40   0.0014131 \\
    196.80  0.0014227 \\
    777.60  0.0014252 \\
    3091.20 0.0014258 \\
    };
\addlegendentry{A}
\addplot [color=blue,line width=1.5pt, smooth]  
table[row sep=crcr]{%
    10.20   0.0014343 \\
    38.40   0.0014285 \\
    148.80  0.0014283 \\
    585.60  0.0014283 \\
    2323.20 0.0014283 \\
    };
\addlegendentry{B}
\end{axis}
\end{tikzpicture}
\end{document}

enter image description here

I would appreciate your help.

1 Answers1

3

This is loosely based on Latex legend in pgfplots. You can determine the width using \pgfextractx etc.

\documentclass{standalone}
\usepackage{tikz}
\usepackage{pgfplots}
\pgfplotsset{compat=1.12}

\newlength{\tempdima}

\begin{document}
\begin{tikzpicture}[]
\begin{axis}[name=border,% this line added
width=15.5cm,
height=6cm,
scaled x ticks=true,
scaled y ticks=false,
x tick label style={
    /pgf/number format/.cd,
    fixed zerofill,
    precision=2,
    use comma,
    1000 sep={},
    /tikz/.cd
},
xticklabel={\ifdim\tick pt=0pt 0 \else\pgfmathprintnumber{\tick}\fi},
y tick label style={
    /pgf/number format/.cd,
    fixed,
    fixed zerofill,
    precision=4,
    use comma,
    1000 sep={},
    /tikz/.cd
},
yticklabel={\ifdim\tick pt=0pt 0 \else\pgfmathprintnumber{\tick}\fi},
xlabel={x-axis},
ylabel={y-axis},
xmin=0,
xmax=1000,
xtick={0,250,500,750,1000},
xticklabels={0,25000,50000,75000,100000},
ymin=0.0012,
ymax=0.0015,
ytick={0.0012,0.0013,0.0014,0.0015},
grid style={line width=.1pt, draw=gray!50},
major grid style={line width=.2pt,draw=gray!80},
grid = both,
minor tick num=1,
legend columns=3,
]   
\addplot [color=black,line width=1.5pt, smooth] 
table[row sep=crcr]{%
    3.60    0.0012304 \\
    13.20   0.0013751 \\
    50.40   0.0014131 \\
    196.80  0.0014227 \\
    777.60  0.0014252 \\
    3091.20 0.0014258 \\
    };
  \label{legend.A}% this line added
\addplot [color=blue,line width=1.5pt, smooth]  
table[row sep=crcr]{%
    10.20   0.0014343 \\
    38.40   0.0014285 \\
    148.80  0.0014283 \\
    585.60  0.0014283 \\
    2323.20 0.0014283 \\
    };
  \label{legend.B}% this line added
\end{axis}
% now the fun begins
\pgfextractx{\tempdima}{\pgfpointdiff{\pgfpointanchor{border}{west}}{\pgfpointanchor{border}{east}}}
\addtolength{\tempdima}{-.666em}% inner sep
\node[draw,inner sep=.333em,above] at (border.north)
  {\makebox[\tempdima]{\ref{legend.A} A\hfil\ref{legend.B} B}};
\end{tikzpicture}
\end{document}

demo

John Kormylo
  • 79,712
  • 3
  • 50
  • 120
  • 1
    Alternative: load the calc library and use \path let \p1=(border.east), \p2=(border.west), \n1={veclen(\x1-\x2,0)} in node[draw,inner xsep=0pt,above] at (border.north) {\makebox[\n1]{\ref{legend.A} A\hfil\ref{legend.B} B}};. (Note that you can set inner xsep separately from y, which means you don't have to subtract twice the inner sep from the width of the box.) – Torbjørn T. Sep 09 '16 at 13:58
  • @TorbjørnT. - Always useful to know alternatives. – John Kormylo Sep 09 '16 at 14:11