如何使图例条目图像由 pgfplots 中的线条、图案和标记组成

如何使图例条目图像由 pgfplots 中的线条、图案和标记组成

我试图让图例条目图像看起来像是以下图“整体”的一部分(因此它有一条线、pattern line一个 s 和一个标记)。这真的可能吗?我尝试调整但没有成功\addlegendimage。这是 mwe(一个听力图模仿者):

\documentclass{standalone}

\usepackage{tikz}
\usetikzlibrary{patterns}
\usepackage{pgfplots}
\usepgfplotslibrary{fillbetween}

\pgfplotsset{
    tick label style={font=\tiny}
}

\begin{document}

\begin{tikzpicture}
\begin{semilogxaxis}[
width=5cm,
xmin=125, xmax=8000,
xtick={125,250,500,750,1000,1500,2000,3000,4000,6000,8000},
xticklabels={0{,}125, 0{,}25, 0{,}5,, 1,, 2,, 4,, 8},
xticklabel pos=upper,
ymin=-10,ymax=120,
ytick={-10, 0, 10, 20, 30, 40, 50, 60, 70, 80, 90, 100, 110, 120},
y dir=reverse,
grid=both,
legend cell align=left,
legend style={draw=none,at={(0,0)},anchor=north west,font=\tiny}]

\addplot+[name path=ucl, red, mark=o, pattern=north east lines, pattern color=red, forget plot]
coordinates {(500,115) (1000,115) (2000,115) (4000,115)};

\path[name path=ucladd] (axis cs:500,120) -- (axis cs:4000,120);

\addplot+[red, mark=o, pattern=north east lines, pattern color=red] fill between[of=ucl and ucladd];
\addlegendentry{UCL}
\end{semilogxaxis}
\end{tikzpicture}

\end{document}

答案1

像这样?

\documentclass{standalone}

\usepackage{tikz}
\usetikzlibrary{patterns}
\usepackage{pgfplots}
\usepgfplotslibrary{fillbetween}


\pgfplotsset{
tick label style={font=\tiny},
compat=1.12,
/pgfplots/my legend/.style={
legend image code/.code={
\begin{scope}[yshift=-0.05cm]
\draw[mark repeat=1,mark phase=1,mark=o,red]
plot coordinates {
(-0.025cm,0.1cm)
(0.3cm,0.1cm)
(0.625cm,0.1cm)
};%
\fill[draw=none,red,pattern=north east lines, pattern color=red] (-0.025cm,0cm) rectangle (0.625cm,0.1cm);
\end{scope}
  }
 }
}

\begin{document}

\begin{tikzpicture}
\begin{semilogxaxis}[
width=5cm,
xmin=125, xmax=8000,
xtick={125,250,500,750,1000,1500,2000,3000,4000,6000,8000},
xticklabels={0{,}125, 0{,}25, 0{,}5,, 1,, 2,, 4,, 8},
xticklabel pos=upper,
ymin=-10,ymax=120,
ytick={-10, 0, 10, 20, 30, 40, 50, 60, 70, 80, 90, 100, 110, 120},
y dir=reverse,
grid=both,
legend cell align=left,
legend style={draw=none,at={(0,0)},anchor=north west,cells={anchor=west},font=\tiny},
]

\addplot+[name path=ucl, red, mark=o, pattern=north east lines, pattern color=red, forget plot]
coordinates {(500,115) (1000,115) (2000,115) (4000,115)};

\path[name path=ucladd] (axis cs:500,120) -- (axis cs:4000,120);

\addplot+[red,  pattern=north east lines, pattern color=red,mark=o,forget plot] fill between[of=ucl and ucladd];
\addlegendimage{
my legend
}
\addlegendentry{UCL}
\end{semilogxaxis}
\end{tikzpicture}

\end{document}

在此处输入图片描述

答案2

虽然有点笨拙,但我认为它可以满足您的需求。有一点需要注意,不要玩弄字体大小,而是尝试缩放覆盖节点。这样可以避免奇怪的数字类型。

\documentclass{standalone}
\usepackage{pgfplots}
\usetikzlibrary{patterns}
\usepgfplotslibrary{fillbetween}
\pgfplotsset{
  tick label style={scale=0.3},
  my custom legend/.style={legend image code/.code={%
    \draw[mark phase=2,##1] plot coordinates {(0cm,0cm) (0cm,1mm)(6mm,1mm)}|-(0cm,0cm);
    }
  }
}

\begin{document}
\begin{tikzpicture}[some option/.style={red, mark=o, pattern=north east lines, pattern color=red}]
\begin{semilogxaxis}[
width=5cm,
xmin=125, xmax=8000,
xtick={125,250,500,750,1000,1500,2000,3000,4000,6000,8000},
xticklabels={0{,}125, 0{,}25, 0{,}5,, 1,, 2,, 4,, 8},
xticklabel pos=upper,
ymin=-10,ymax=120,
ytick={-10, 0, ..., 120},
y dir=reverse,grid=both,legend cell align=left,
legend style={draw=none,at={(0,0)},anchor=north west,nodes={anchor=text,scale=0.5}}
]
\addplot+[name path=ucl, some option, forget plot]coordinates {(500,115)(1000,115)(2000,115)(4000,115)};
\path[name path=ucladd] (axis cs:500,120) -- (axis cs:4000,120);
\addplot+[some option,my custom legend] fill between[of=ucl and ucladd];
\addlegendentry{UCL}
\end{semilogxaxis}
\end{tikzpicture}
\end{document}

在此处输入图片描述

相关内容