将矩形图像添加到 pgf 图例中

将矩形图像添加到 pgf 图例中

我正在制作一个相当简单tikzpicture的 pgfplot 和 sketch 的组合。代码如下

\documentclass[border=10pt,tikz]{standalone}
\usepackage{pgfplots}
\usetikzlibrary{calc}
\usetikzlibrary{patterns}
\usetikzlibrary{fadings}
\pgfplotsset{compat=1.14}

\begin{document}
\begin{tikzpicture}
\begin{axis}[
    ultra thick,
    grid=both,
    grid style={line width=.1pt, draw=gray!30, dashed},
    domain=100:600,
    xmin=100, xmax=600,
    xtick={100, 200, ..., 600},
    ymin=-300, ymax=40,
    ytick={0, -50, ..., -300},
    xlabel=Time,
    ylabel=Amplitude,
    each nth point=10, 
    filter discard warning=false, 
    unbounded coords=discard,
    legend style={nodes={scale=0.8, transform shape},
                  draw={none}, 
                  fill={none},
                  at={(0.45,0.205)},
                  anchor=north west
                  },
    ]
    \addplot[smooth, thick] table {FINAL.dat}
        node[right] {Forward emission};
    \addplot[smooth, thick, dashed] table {Single_Medium.dat}
        node[above right] {Intermediate emission};
    \addplot[smooth, thick, dotted] table {Single_SuperNarrow.dat}
        node[above right] {Large angle emission};
    \legend{Forward emission, Intermediate emission, Large angle emission};
    %\addlegendimage{foil, \draw[pattern=north west lines] (0,0) rectangle (1,1)};
    %\addlegendentry{foil, Foil};
\end{axis}
%box
\draw[fill=white] (3.7, 4.55) rectangle (6.65, 1.45);
%\draw[gray, <->] (5.175, 4.5) -- (5.175, 1.5); 
%\draw[gray, <->] (3.75, 3) -- (6.6, 3);
%gas
\fill[fill=gray!30, path fading=north] (3.75, 4.4) rectangle (6.6, 3);
% fission foil
\draw[pattern=north west lines] (5.175-0.5, 4.5) rectangle (5.175+0.5, 4.4);
%Mesh
\draw[pattern=north east lines] (3.75, 3) rectangle (6.6, 2.8);
\draw[fill=white] (3.75+0.25, 3) rectangle (6.6-0.25, 2.8);
%tracks
\draw[->] (5.175, 4.4) -- (5.175, 3);
\draw[->, dotted] (5.175, 4.4) -- (6.5, 4.35);
\draw[->, dashed] (5.175, 4.4) -- (5.8, 3.1);
\draw[->, dashed] (5.175, 4.4) -- (3.8, 3.1);
\end{tikzpicture}
\end{document}

产生

在此处输入图片描述

我正在尝试使用图例添加所有内容\addlegendimage,并\addlegendentry描述在框内看到的填充有图案/淡入淡出的矩形。

例如灰色褪色的盒子应该有一个条目描述它是一种气体,用图案绘制的裂变箔应该有它的描述等等。

我尝试使用

%\addlegendimage{\draw[pattern=north west lines] (0,0) rectangle (1,1)};
%\addlegendentry{Foil};

但没用。有什么办法吗?

答案1

\addlegendimage例如,仅添加样式选项,而不是整个路径

\addlegendimage{area legend,pattern=north west lines}

在此处输入图片描述

\documentclass[border=10pt,tikz]{standalone}
\usepackage{pgfplots}
\usetikzlibrary{calc}
\usetikzlibrary{patterns}
\usetikzlibrary{fadings}
\pgfplotsset{compat=1.14}

\begin{document}
\begin{tikzpicture}
\begin{axis}[
    ultra thick,
    grid=both,
    grid style={line width=.1pt, draw=gray!30, dashed},
    domain=100:600,
    xmin=100, xmax=600,
    xtick={100, 200, ..., 600},
    ymin=-300, ymax=40,
    ytick={0, -50, ..., -300},
    xlabel=Time,
    ylabel=Amplitude,
    each nth point=10, 
    filter discard warning=false, 
    unbounded coords=discard,
    legend style={nodes={scale=0.8, transform shape},
                  draw={none}, 
                  fill={none},
                  at={(0.45,0.255)},
                  anchor=north west
                  },
    ]
    \addplot[smooth, thick]  {rnd} %table {FINAL.dat}
        node[right] {Forward emission};
    \addplot[smooth, thick, dashed] {rnd} %table {Single_Medium.dat}
        node[above right] {Intermediate emission};
    \addplot[smooth, thick, dotted] {rnd} %table {Single_SuperNarrow.dat}
        node[above right] {Large angle emission};

    \addlegendimage{area legend,pattern=north west lines}

    \legend{Forward emission, Intermediate emission, Large angle emission, Foobar};
    %\addlegendimage{foil, \draw[pattern=north west lines] (0,0) rectangle (1,1)};
    %\addlegendentry{foil, Foil};
\end{axis}
%box
\draw[fill=white] (3.7, 4.55) rectangle (6.65, 1.45);
%\draw[gray, <->] (5.175, 4.5) -- (5.175, 1.5); 
%\draw[gray, <->] (3.75, 3) -- (6.6, 3);
%gas
\fill[fill=gray!30, path fading=north] (3.75, 4.4) rectangle (6.6, 3);
% fission foil
\draw[pattern=north west lines] (5.175-0.5, 4.5) rectangle (5.175+0.5, 4.4);
%Mesh
\draw[pattern=north east lines] (3.75, 3) rectangle (6.6, 2.8);
\draw[fill=white] (3.75+0.25, 3) rectangle (6.6-0.25, 2.8);
%tracks
\draw[->] (5.175, 4.4) -- (5.175, 3);
\draw[->, dotted] (5.175, 4.4) -- (6.5, 4.35);
\draw[->, dashed] (5.175, 4.4) -- (5.8, 3.1);
\draw[->, dashed] (5.175, 4.4) -- (3.8, 3.1);
\end{tikzpicture}
\end{document}

相关内容