我正在用 Beamer 工作pgfplotstable
。我想制作以下内容:
\documentclass{beamer}
\usepackage{tikz,pgfplots,pgfplotstable,filecontents}
\pgfplotsset{compat=newest}
\beamertemplatenavigationsymbolsempty
\begin{document}
\begin{frame}
\centering
\begin{tikzpicture}
\begin{axis}[
xmin=0, xmax=5, ymin=0, ymax=5,
ytick={0,1,...,5}, yticklabels={0,1,...,5},
ytick style={draw=none},
xtick={0,1,...,5}, xticklabels={0,1,...,5},
xtick style={draw=none},
axis lines*=left,
ylabel={y},
xlabel={x}
]
\draw[ultra thick, cyan] (1,0)--(1,3) (2,0)--(2,4);
\end{axis}
\end{tikzpicture}
\end{frame}
\end{document}
但使用循环遍历 .dat 文件的元素。我的尝试如下,但它会抛出错误。到目前为止,我发现这篇类似的帖子,但不明白为什么他们的有效而我的无效。如果能提供任何指点,我将不胜感激。
\documentclass{beamer}
\usepackage{tikz,pgfplots,pgfplotstable,filecontents}
\pgfplotsset{compat=newest}
\beamertemplatenavigationsymbolsempty
\begin{filecontents*}{mwe.dat}
1 3
2 4
\end{filecontents*}
\begin{document}
\begin{frame}
\centering
\begin{tikzpicture}
\begin{axis}[
xmin=0, xmax=5, ymin=0, ymax=5,
ytick={0,1,...,5}, yticklabels={0,1,...,5},
ytick style={draw=none},
xtick={0,1,...,5}, xticklabels={0,1,...,5},
xtick style={draw=none},
axis lines*=left,
ylabel={y},
xlabel={x}
]
\pgfplotstableread[header=false]{mwe.dat}{\mwe}
\foreach \i in {0,1}{
\pgfplotstablegetelem{\i}{[index]1}\of\mwe
\pgfmathsetmacro{\y}{\pgfplotsretval}
\draw[ultra thick, cyan] (\i,0)--(\i,\y);
}
\end{axis}
\end{tikzpicture}
\end{frame}
\end{document}
答案1
欢迎!您的方法有效,但在轴上,必须对扩展稍微小心一点,请参阅第节8.1 实用命令pgfplots v1.17。
\documentclass{beamer}
\usepackage{pgfplots,pgfplotstable,filecontents}
\pgfplotsset{compat=newest}
\beamertemplatenavigationsymbolsempty
\begin{filecontents*}{mwe.dat}
1 3
2 4
\end{filecontents*}
\begin{document}
\begin{frame}
\centering
\begin{tikzpicture}
\begin{axis}[
xmin=0, xmax=5, ymin=0, ymax=5,
ytick={0,1,...,5}, yticklabels={0,1,...,5},
ytick style={draw=none},
xtick={0,1,...,5}, xticklabels={0,1,...,5},
xtick style={draw=none},
axis lines*=left,
ylabel={y},
xlabel={x}
]
\pgfplotstableread[header=false]{mwe.dat}{\mwe}
\foreach \i in {0,1}{
\pgfplotstablegetelem{\i}{[index]1}\of\mwe
\pgfmathsetmacro{\y}{\pgfplotsretval}
\edef\temp{\noexpand\draw[ultra thick, cyan] (\i+1,0)--(\i+1,\y);}
\temp
}
\end{axis}
\end{tikzpicture}
\end{frame}
\end{document}
当然,为此您只需使用 即可ycomb
。
\documentclass{beamer}
\usepackage{pgfplots,pgfplotstable,filecontents}
\pgfplotsset{compat=newest}
\beamertemplatenavigationsymbolsempty
\begin{filecontents*}{mwe.dat}
1 3
2 4
\end{filecontents*}
\begin{document}
\begin{frame}
\centering
\begin{tikzpicture}
\begin{axis}[
xmin=0, xmax=5, ymin=0, ymax=5,
ytick={0,1,...,5}, yticklabels={0,1,...,5},
ytick style={draw=none},
xtick={0,1,...,5}, xticklabels={0,1,...,5},
xtick style={draw=none},
axis lines*=left,
ylabel={y},
xlabel={x}
]
\addplot[ycomb,ultra thick, cyan] table {mwe.dat};
\end{axis}
\end{tikzpicture}
\end{frame}
\end{document}