基于问题和回答我已经为此做好了准备;现在我有一个问题,关于如何在利用 pgfplots 的分层功能制作的图中添加图例。
至于图例选项是在axis
环境中定义的;并且图层功能允许组合多个单独的axis
环境,如下所示:
但我找不到像这样为整个情节添加图例的方法:
如何为整个 pgfplots 图层图添加完整的图例。
\documentclass[margin=1mm]{standalone}
\usepackage{filecontents}
\usepackage{pgfplots}
\usepgfplotslibrary{statistics}
\begin{document}
\begin{tikzpicture}[
declare function={gamma(\z)= 2.506628274631*sqrt(1/\z)+ 0.20888568*(1/\z)^(1.5)+ 0.00870357*(1/\z)^(2.5)- (174.2106599*(1/\z)^(3.5))/25920- (715.6423511*(1/\z)^(4.5))/1244160)*exp((-ln(1/\z)-1)*\z;},
declare function={gammapdf(\x,\k,\theta) = 1/(\theta^\k)*1/(gamma(\k))*\x^(\k-1)*exp(-\x/\theta);},
declare function={cauchypdf(\x,\mu,\gamma) = 1/(pi*\gamma*(1+((\x-\mu)/\gamma)^2));}
]
\pgfplotsset{set layers}
\begin{axis}[
axis y line*=left,
ylabel=Histogram,
every axis y label/.style={rotate=90, blue, at={(-0.15,0.5)},},
ybar,
ymin=0,
yticklabel style={blue},
]
\addplot +[hist] table [y index=0, col sep=comma,row sep=newline] {
data
48
51
48
51
66
0
0
25
31
60
9
43
49
15
22
1
};
\end{axis}
\begin{axis}[
ylabel style={black},
axis y line*=right,
axis x line=none,
ylabel=PDF's,
every axis y label/.style={rotate=-90, black, at={(1.15,0.5)},},
/pgfplots/every y tick scale label/.style={
at={(1,1)},
above right,
inner sep=0pt,
yshift=0.3em,
},
yticklabel style={black},
legend entries={
$\textbf{Gamma}~\alpha=1.7435~ \beta=21.263$,
$~\textbf{Cauchy}~\sigma=16.1340~ \mu=39.554$
},
legend style={at={(0.5,-0.15)},anchor=north},
ymin=0,
]
\addplot [thick, smooth, domain=0:66, black, mark=square] {gammapdf(x,1.7435, 21.263)};
\addplot [thick, smooth, domain=0:66, red, mark=triangle] {cauchypdf(x,39.554, 16.134)};
\end{axis}
\end{tikzpicture}
\end{document}
答案1
您可以标记每个图,然后将图例设置为轴环境之外的 tikz 矩阵。
\documentclass[margin=1mm]{standalone}
\usepackage{pgfplots}
\usepgfplotslibrary{statistics}
\usetikzlibrary{matrix}
\begin{document}
\begin{tikzpicture}[
declare function={gamma(\z)= 2.506628274631*sqrt(1/\z)+ 0.20888568*(1/\z)^(1.5)+ 0.00870357*(1/\z)^(2.5)- (174.2106599*(1/\z)^(3.5))/25920- (715.6423511*(1/\z)^(4.5))/1244160)*exp((-ln(1/\z)-1)*\z;},
declare function={gammapdf(\x,\k,\theta) = 1/(\theta^\k)*1/(gamma(\k))*\x^(\k-1)*exp(-\x/\theta);},
declare function={cauchypdf(\x,\mu,\gamma) = 1/(pi*\gamma*(1+((\x-\mu)/\gamma)^2));}
]
\pgfplotsset{set layers}
\begin{axis}[
axis y line*=left,
ylabel=Histogram,
every axis y label/.style={rotate=90, blue, at={(-0.15,0.5)},},
ybar,
ymin=0,
yticklabel style={blue},
]
\addplot +[hist] table [y index=0, col sep=comma,row sep=newline] {
data
48
51
48
51
66
0
0
25
31
60
9
43
49
15
22
1
};\label{plots:hist}
\end{axis}
\begin{axis}[
ylabel style={black},
axis y line*=right,
axis x line=none,
ylabel=PDF's,
every axis y label/.style={rotate=-90, black, at={(1.15,0.5)},},
/pgfplots/every y tick scale label/.style={
at={(1,1)},
above right,
inner sep=0pt,
yshift=0.3em,
},
yticklabel style={black},
ymin=0,
]
\addplot [thick, smooth, domain=0:66, black, mark=square] {gammapdf(x,1.7435, 21.263)};
\label{plots:gamma}
\addplot [thick, smooth, domain=0:66, red, mark=triangle] {cauchypdf(x,39.554, 16.134)};
\label{plots:cauchy}
\end{axis}
% Legend
\matrix[
matrix of nodes,
draw,
anchor=north,
column 1/.style={nodes={anchor=center}},
column 2/.style={nodes={anchor=west}}
] at([yshift=-2.5em]current axis.south)
{
\ref{plots:gamma}&$\textbf{Gamma}~\alpha=1.7435~ \beta=21.263$\\
\ref{plots:cauchy}&$\textbf{Cauchy}~\sigma=16.1340~ \mu=39.554$\\
\ref{plots:hist}&Histogram\\};
\end{tikzpicture}
\end{document}
因为 Iony 的评论:
您可以使用Ijon Tichy 建议的补丁在www.texwelt.de将其外部化tikzpicture
:
%%% %http://texwelt.de/wissen/fragen/9476/labels-an-pgfplots/9527 (by Ijon Tichy)
\usepackage{scrlfile}
\usepackage{etoolbox}
\makeatletter
\newif\if@lateexternal
\newcommand*{\nextwithlateexternal}{\@lateexternaltrue}
\renewcommand*{\@lateexternalfalse}{\global\let\if@lateexternal\iffalse}
% Den Systemaufruf von external so ändern, dass er optional doppelt
% stattfindet: Zunächst wie gehabt unmittelbar und zusätzlich nachdem
% die aux-Datei geschlossen (und sogar neu gelesen) wurde.
\patchcmd\tikzexternal@externalizefig@systemcall@@
{\immediate\write18{\pgf@tempa}}%
{\immediate\write18{\pgf@tempa}%
\if@lateexternal
\begingroup
\protected@edef\reserved@a{%
\noexpand\endgroup
\noexpand\AfterReadingMainAux{%
\noexpand\immediate\noexpand\write18{%
\expandafter\detokenize\expandafter{\pgf@tempa}}%
}%
}%
\reserved@a
\fi
}%
{}%
{\patchFailedError}
% Nun dafür sorgen, dass der Aufruf \nextwithlateexternal nur auf
% den nächsten potentiellen Systemaufruf von external wirkt statt
% auf den nächsten tatsächlichen oder gar alle:
\apptocmd\tikzexternal@externalizefig@systemcall@@
{\@lateexternalfalse}
{}
{\patchFailedError}
\makeatother
%%%
然后\nextwithlateexternal
立即在该tikzpicture
环境之前。使用 运行两次-shell-escape
。
第一次运行:
第二次运行:
代码:
\documentclass[margin=1mm]{standalone}
\usepackage{pgfplots}
\usepgfplotslibrary{statistics}
\usetikzlibrary{matrix}
\usepgfplotslibrary{external}
\tikzexternalize
\tikzsetexternalprefix{external_figs/}
%%% %http://texwelt.de/wissen/fragen/9476/labels-an-pgfplots/9527 (by Ijon Tichy)
\usepackage{scrlfile}
\usepackage{etoolbox}
\makeatletter
\newif\if@lateexternal
\newcommand*{\nextwithlateexternal}{\@lateexternaltrue}
\renewcommand*{\@lateexternalfalse}{\global\let\if@lateexternal\iffalse}
% Den Systemaufruf von external so ändern, dass er optional doppelt
% stattfindet: Zunächst wie gehabt unmittelbar und zusätzlich nachdem
% die aux-Datei geschlossen (und sogar neu gelesen) wurde.
\patchcmd\tikzexternal@externalizefig@systemcall@@
{\immediate\write18{\pgf@tempa}}%
{\immediate\write18{\pgf@tempa}%
\if@lateexternal
\begingroup
\protected@edef\reserved@a{%
\noexpand\endgroup
\noexpand\AfterReadingMainAux{%
\noexpand\immediate\noexpand\write18{%
\expandafter\detokenize\expandafter{\pgf@tempa}}%
}%
}%
\reserved@a
\fi
}%
{}%
{\patchFailedError}
% Nun dafür sorgen, dass der Aufruf \nextwithlateexternal nur auf
% den nächsten potentiellen Systemaufruf von external wirkt statt
% auf den nächsten tatsächlichen oder gar alle:
\apptocmd\tikzexternal@externalizefig@systemcall@@
{\@lateexternalfalse}
{}
{\patchFailedError}
\makeatother
%%%
\begin{document}
\nextwithlateexternal% <- added
\begin{tikzpicture}[
declare function={gamma(\z)= 2.506628274631*sqrt(1/\z)+ 0.20888568*(1/\z)^(1.5)+ 0.00870357*(1/\z)^(2.5)- (174.2106599*(1/\z)^(3.5))/25920- (715.6423511*(1/\z)^(4.5))/1244160)*exp((-ln(1/\z)-1)*\z;},
declare function={gammapdf(\x,\k,\theta) = 1/(\theta^\k)*1/(gamma(\k))*\x^(\k-1)*exp(-\x/\theta);},
declare function={cauchypdf(\x,\mu,\gamma) = 1/(pi*\gamma*(1+((\x-\mu)/\gamma)^2));}
]
\pgfplotsset{set layers}
\begin{axis}[
axis y line*=left,
ylabel=Histogram,
every axis y label/.style={rotate=90, blue, at={(-0.15,0.5)},},
ybar,
ymin=0,
yticklabel style={blue},
]
\addplot +[hist] table [y index=0, col sep=comma,row sep=newline] {
data
48
51
48
51
66
0
0
25
31
60
9
43
49
15
22
1
};\label{plots:hist}
\end{axis}
\begin{axis}[
ylabel style={black},
axis y line*=right,
axis x line=none,
ylabel=PDF's,
every axis y label/.style={rotate=-90, black, at={(1.15,0.5)},},
/pgfplots/every y tick scale label/.style={
at={(1,1)},
above right,
inner sep=0pt,
yshift=0.3em,
},
yticklabel style={black},
ymin=0,
]
\addplot [thick, smooth, domain=0:66, black, mark=square] {gammapdf(x,1.7435, 21.263)};
\label{plots:gamma}
\addplot [thick, smooth, domain=0:66, red, mark=triangle] {cauchypdf(x,39.554, 16.134)};
\label{plots:cauchy}
\end{axis}
% Legend
\matrix[
matrix of nodes,
draw,
anchor=north,
column 1/.style={nodes={anchor=center}},
column 2/.style={nodes={anchor=west}}
] at([yshift=-2.5em]current axis.south)
{
\ref{plots:gamma}&$\textbf{Gamma}~\alpha=1.7435~ \beta=21.263$\\
\ref{plots:cauchy}&$\textbf{Cauchy}~\sigma=16.1340~ \mu=39.554$\\
\ref{plots:hist}&Histogram\\};
\end{tikzpicture}
\end{document}