使用 PGFplots 时,我想对图应用装饰,但不对相应的图例条目应用装饰。有办法吗?
这里有一个例子:我希望图例图像只是一条线,没有装饰“测试”。
\documentclass{standalone}
\usepackage{tikz,pgfplots}
\usetikzlibrary{decorations.markings}
\pgfplotsset{compat=1.12}
\makeatletter
\tikzset{reset postactions/.code=\let\tikz@postactions\pgfutil@empty}
\makeatother
\tikzset{test decoration/.style={
postaction={
decorate,
decoration={
markings,
mark=between positions 0.5 and 1 step 10mm with {\node {test};}
}
}
}}
\begin{document}
\begin{tikzpicture}
\begin{axis}[legend image post style={reset postactions}]
\addplot[blue,test decoration] {-x};
\legend{$-x$};
\end{axis}
\end{tikzpicture}
\end{document}
风格reset postactions
(无效)来自于这个答案。我也尝试过将test decoration
样式重置为内部为空legend image post style
,但没有效果。
答案1
一种解决方法是使用forget plot
和\addlegendimage
。
\documentclass{standalone}
\usepackage{tikz,pgfplots}
\usetikzlibrary{decorations.markings}
\pgfplotsset{compat=1.12}
\tikzset{test decoration/.style={
postaction={
decorate,
decoration={
markings,
mark=between positions 0.5 and 1 step 10mm with {\node {test};}
}
}
}}
\begin{document}
\begin{tikzpicture}
\begin{axis}[myplot/.style={blue}]
\addplot[myplot,test decoration,forget plot] {-x};
\addlegendimage{myplot}
\legend{$-x$};
\end{axis}
\end{tikzpicture}
\end{document}