tikz
我以为我可以通过为图片添加图例\addlegendentry{...}
。但是,只要我取消注释该命令,编译器就会抱怨缺少}
。但是当我再次注释这些行时,它就可以正常工作了。有人可以给我提示一下,为什么图例不起作用?
\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{arrows, intersections}
\usepackage{pgfplots}
\pgfplotsset{compat=newest}
\pgfplotsset{plot coordinates/math parser=false}
%from matlab2tikz
\newlength\figureheight
\newlength\figurewidth
\begin{document}
Sample plot with out legend?
\begin{figure}
\setlength\figureheight{2cm}
\setlength\figurewidth{2.7cm}
{
\begin{tikzpicture}[font=\tiny]
\begin{axis}[%
width=\figurewidth,
height=\figureheight,
scale only axis,
xmin=1,
xmax=20,
ymin=0,
ymax=2,
title={Sample time series},
legend style={draw=black,fill=white,legend cell align=left}
]
\addplot [color=blue,solid,forget plot]
table[row sep=crcr]{1 1\\
2 1\\
3 1\\
4 1\\
5 1\\
6 1\\
7 1\\
8 1\\
9 1\\
10 2\\
11 1\\
12 1.5\\
13 0\\
14 1\\
15 1\\
16 1\\
17 1\\
18 1\\
19 1\\
20 1\\
};
\addplot [color=green,dashed ,forget plot]
table[row sep=crcr]{8 0\\
8 2\\
10 2\\
10 0\\
};
%\addlegendentry{Window size=2};
\addplot [color=violet,densely dotted ,forget plot]
table[row sep=crcr]{10.5 0\\
10.5 2\\
15.5 2\\
15.5 0\\
};
%\addlegendentry{Window size=5};
\end{axis}
\end{tikzpicture}%
}
\end{figure}
\end{document}
答案1
不要forget plot
用于您想要添加图例的图表(forget plot
适用于不应记住图例条目也不应考虑循环列表的图表):
\documentclass[twoside]{article}
\usepackage{pgfplots}
\newlength\figurewidth
\setlength\figurewidth{6cm}
\newlength\figureheight
\setlength\figureheight{6cm}
\begin{document}
\begin{tikzpicture}[font=\tiny]
\begin{axis}[%
width=\figurewidth,
height=\figureheight,
scale only axis,
xmin=1,
xmax=20,
ymin=0,
ymax=2,
title={Sample time series},
legend style={draw=black,fill=white,legend cell align=left}
]
\addplot [color=blue,solid,forget plot]
table[row sep=crcr]{1 1\\
2 1\\
3 1\\
4 1\\
5 1\\
6 1\\
7 1\\
8 1\\
9 1\\
10 2\\
11 1\\
12 1.5\\
13 0\\
14 1\\
15 1\\
16 1\\
17 1\\
18 1\\
19 1\\
20 1\\
};
\addplot [color=green,dashed ]
table[row sep=crcr]{8 0\\
8 2\\
10 2\\
10 0\\
};
\addlegendentry{Window size=2}
\addplot [color=violet,densely dotted ]
table[row sep=crcr]{10.5 0\\
10.5 2\\
15.5 2\\
15.5 0\\
};
\addlegendentry{Window size=5}
\end{axis}
\end{tikzpicture}%
\end{document}