在 beamer 中使用 pgfplots

在 beamer 中使用 pgfplots

我想在一张幻灯片中绘制以下数据

%-----------------------
\documentclass{beamer}

\usepackage{tikz}
\usepackage{pgfplots}
\usepackage{filecontents}

\begin{document}

\begin{frame}[t]
\frametitle{Numerical Results}
\begin{filecontents}{Sp.dat}
n   r     a        k 
1          100          100          100
2       95.721       98.645       98.738
4        82.31       93.497       93.295
8       56.667       63.901       63.838
16       46.536       52.572        53.31
32       45.399       53.421       53.318
\end{filecontents}

\pgfplotstableread{Sp.dat}{\Sp}
\pgfplotsset{tick label style={font=\tiny\bfseries},
label style={font=\small},
legend style={font=\tiny}
}
\begin{tikzpicture}[scale=1.0]
\begin{axis}[ymajorgrids=true,
 xmin=0,xmax=32,ymin=40,ymax=105,
xtick={1,2,4,8,16,32},
ytick={45,50,60,70,80,90,100},
xlabel= Number of processors,
ylabel=Efficiency]
\addplot [black,very thick] table [x={n}, y={r}] {\Sp};
\addlegendentry{$20\rightarrow 15$}
\addplot [dashed,red,very thick] table [x={n}, y={n}] {\Sp};
\addlegendentry{$20\rightarrow 10$}
\addplot [dotted,blue,very thick] table [x={n}, y={k}] {\Sp};
\addlegendentry{$20\rightarrow 5$}
\end{axis}
\end{tikzpicture}
\end{figure}
\end{frame}
%---------------------

\end{document}

但错误是

! Package pgfplots Error: Sorry, could not retrieve column 'n' from table 'Sp.d
at'. Please check spelling (or introduce name aliases)..

See the pgfplots package documentation for explanation.
Type  H <return>  for immediate help.
 ...                                              

l.1718 \end{frame}

答案1

将选项添加fragile到框架:

\documentclass{beamer}

\usepackage{tikz}
\usepackage{pgfplots}
\usepackage{filecontents}

\begin{document}

%-----------------------
\begin{frame}[t,fragile]
\frametitle{Numerical Results}
\begin{filecontents}{Sp.dat}
n   r     a        k 
1          100          100          100
2       95.721       98.645       98.738
4        82.31       93.497       93.295
8       56.667       63.901       63.838
16       46.536       52.572        53.31
32       45.399       53.421       53.318
\end{filecontents}
\pgfplotstableread{Sp.dat}{\Sp}
\pgfplotsset{tick label style={font=\tiny\bfseries},
label style={font=\small},
legend style={font=\tiny}
}
\begin{tikzpicture}[scale=1.0]
\begin{axis}[ymajorgrids=true,
 xmin=0,xmax=32,ymin=40,ymax=105,
xtick={1,2,4,8,16,32},
ytick={45,50,60,70,80,90,100},
xlabel= Number of processors,
ylabel=Efficiency]
\addplot [black,very thick] table [x={n}, y={r}] {\Sp};
\addlegendentry{$20\rightarrow 15$}
\addplot [dashed,red,very thick] table [x={n}, y={n}] {\Sp};
\addlegendentry{$20\rightarrow 10$}
\addplot [dotted,blue,very thick] table [x={n}, y={k}] {\Sp};
\addlegendentry{$20\rightarrow 5$}
\end{axis}
\end{tikzpicture}
\end{frame}
%---------------------

\end{document}

在此处输入图片描述

或者将filecontents环境移到环境之外frame;例如,您可以将其放在序言中:

\documentclass{beamer}

\usepackage{tikz}
\usepackage{pgfplots}
\usepackage{filecontents}

\begin{filecontents}{Sp.dat}
n   r     a        k 
1          100          100          100
2       95.721       98.645       98.738
4        82.31       93.497       93.295
8       56.667       63.901       63.838
16       46.536       52.572        53.31
32       45.399       53.421       53.318
\end{filecontents}

\begin{document}

%-----------------------
\begin{frame}[t]
\frametitle{Numerical Results}
\pgfplotstableread{Sp.dat}{\Sp}
\pgfplotsset{tick label style={font=\tiny\bfseries},
label style={font=\small},
legend style={font=\tiny}
}
\begin{tikzpicture}[scale=1.0]
\begin{axis}[ymajorgrids=true,
 xmin=0,xmax=32,ymin=40,ymax=105,
xtick={1,2,4,8,16,32},
ytick={45,50,60,70,80,90,100},
xlabel= Number of processors,
ylabel=Efficiency]
\addplot [black,very thick] table [x={n}, y={r}] {\Sp};
\addlegendentry{$20\rightarrow 15$}
\addplot [dashed,red,very thick] table [x={n}, y={n}] {\Sp};
\addlegendentry{$20\rightarrow 10$}
\addplot [dotted,blue,very thick] table [x={n}, y={k}] {\Sp};
\addlegendentry{$20\rightarrow 5$}
\end{axis}
\end{tikzpicture}
\end{frame}
%---------------------

\end{document}

相关内容