我试图在以下tikzpicture
代码中隐藏图例。但是,使用后every axis legend/.code={\let\addlegendentry\relax}
,图片变得混乱。有人能帮我解决这个问题吗?
\documentclass[border=5mm]{standalone}
\usepackage{pgfplots}
\pgfplotsset{compat=1.8}
\begin{document}
\begin{tikzpicture}
\begin{axis}[
width=10cm,
height=5cm,
scale only axis,
xmin=0.8, xmax=3.2,
xtick={1,2,3},
xmajorgrids,
ymin=0, ymax=1,
ylabel={$\phi$},
ymajorgrids,
axis lines*=left,
legend style ={ at={(1.03,1)},
anchor=north west, draw=black,
fill=white,align=left},
cycle list name=black white,
smooth,
every axis legend/.code={\let\addlegendentry\relax},
]
\addplot coordinates{
(1,0.35)
(2,0.41)
(3,0.49)
};
\addlegendentry{$S1$=2.5};
\addplot coordinates{
(1,0.32)
(2,0.46)
(3,0.61)
};
\addlegendentry{$S2$=3.5};
\addplot coordinates{
(1,0.31)
(2,0.46)
(3,0.51)
};
\addlegendentry{$S3$=4.0};
\addplot coordinates{
(1,0.20)
(2,0.33)
(3,0.58)
};
\addlegendentry{$S4$=4.5};
\addplot coordinates{
(1,0.25)
(2,0.42)
(3,0.48)
};
\addlegendentry{$S5$=5.0};
\addplot coordinates{
(1,0.32)
(2,0.40)
(3,0.58)
};
\addlegendentry{$S6$=6.0};
\end{axis}
\end{tikzpicture}%
\end{document}
这是上述代码的输出。
答案1
不要使用\relax
,因为 会删除 的参数\addlegendentry{...}
并导致它们被错误地设置为轴原点处的文本,而是\legend{}
在环境末尾使用 (empty) axis
。这会清空图例,因此不会在输出中排版。
我在这里简化了你的 MWE;将来,请只显示与你的问题相关的代码。
\documentclass{standalone}
\usepackage{pgfplots}
\pgfplotsset{compat=1.10}
\begin{document}
\begin{tikzpicture}
\begin{axis}
\addplot {x};
\addlegendentry{foo};
\addplot {x^2};
\addlegendentry{bar};
\legend{}; % empty the legend so as not to print it
\end{axis}
\end{tikzpicture}
\end{document}