当我尝试改变曲线的显示顺序并同时保持图例条目顺序时遇到了一些问题。
曲线顺序按预期发生了变化,但它们超出了轴的限制。以下是 MWE:
\documentclass{article}
\usepackage{pgfplots}
\begin{document}
\begin{tikzpicture}
\pgfdeclarelayer{A}
\pgfdeclarelayer{B}
\pgfdeclarelayer{C}
\pgfplotsset{/pgfplots/layers/my layers/.define layer set={main,A,B,C}{}}
\begin{axis}[xmax=9,ymin=0,set layers=my layers]
\addplot[line width=3pt,red,on layer=A] coordinates {(0,10) (10,0)};
\addplot[line width=3pt,green,on layer=C] coordinates {(0,9) (10,1)};
\addplot[line width=3pt,blue,on layer=B] coordinates {(0,8) (10,2)};
\legend{case 1,case 2,case 3}
\end{axis}
\end{tikzpicture}
\end{document}
及其结果:
我究竟做错了什么?
答案1
诀窍是添加clip mode=individual
选项axis
。
(除此之外,这些\pgfdeclarelayer
命令是多余的,因为它们.define layer set
无论如何都会被调用。)
% used PGFPlots v1.17
\documentclass[border=5pt]{standalone}
\usepackage{pgfplots}
\usepgfplotslibrary{fillbetween}
\begin{document}
\begin{tikzpicture}
\pgfplotsset{
/pgfplots/layers/my layers/.define layer set={main,A,B,C}{},
}
\begin{axis}[
set layers=my layers,
xmax=9,
ymin=0,
clip mode=individual,
]
\addplot [line width=3pt,red, on layer=A] coordinates {(0,10) (10,0)};
\addplot [line width=3pt,green,on layer=C] coordinates {(0,9) (10,1)};
\addplot [line width=3pt,blue, on layer=B] coordinates {(0,8) (10,2)};
\legend{case 1,case 2,case 3}
\end{axis}
\end{tikzpicture}
\end{document}