我想将图例框的宽度放大到图框的宽度。结果应该是这样的:
我尝试使用以下方法来调整宽度
legend style={minimum width=xx cm},
但这不能正常工作,我必须手动计算出每个图的宽度。
\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}
我将非常感谢您的帮助。
答案1
这是基于pgfplots 中的 Latex 图例。\pgfextractx
您可以使用等 确定宽度。
\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}