pgfplots:显示来自同一输入文件的条形图和表格

pgfplots:显示来自同一输入文件的条形图和表格

我尝试在一张 Beamer 幻灯片上显示一个条形图和一个表格,其中的数字取自同一个输入文件(以避免在复制数据时出现重复)。目前,我被困在这个错误消息上:

PGFPlots: reading {benchmarks.data}
! TeX capacity exceeded, sorry [input stack size=5000].
\@setfontsize #1#2#3->\@nomath #1
                        \ifx \protect \@typeset@protect \let \@curr...
l.33 \end{frame}

我的数据文件如下:

paradigm                                          position  time
{\scriptsize \shortstack{Procedural\\(append)}}     1       53
{\scriptsize \shortstack{Procedural\\(prepend)}}    2       64
{\scriptsize Object-Oriented}                       3       45
{\scriptsize Functional}                            4       44
{\scriptsize List Comprehension}                    5       30

下面是我的 LaTeX 代码的精简版:

\documentclass[10pt]{beamer}
\usepackage{pgfplots,pgfplotstable}

\begin{document}

\begin{frame}
  \frametitle{Benchmark Results}
  \begin{columns}[m]
    \begin{column}{.5\textwidth}
      \begin{tikzpicture}[scale=.75]
        \begin{axis}[%
          ybar, bar width=0.65cm,%
          xmin=0.5,xmax=5.5, xtick=data,%
          xticklabels from table={benchmarks.data}{paradigm},%
          xticklabel style={rotate=45,anchor=north east,inner sep=0mm},%
          ylabel={\Large Time (s)}, ylabel near ticks]
          \addplot table [x=position,y=time] {benchmarks.data};
        \end{axis}
      \end{tikzpicture}      
    \end{column}
    \begin{column}{.35\textwidth}
      \pgfplotstabletypesetfile[%
      every even row/.style={before row={\rowcolor{blue!15}}},
      every head row/.style={before row=\toprule, after row=\midrule},
      every last row/.style={after row=\bottomrule},
      columns/paradigm/.style={column name={Paradigm}},
      columns/position/.style={column name={Position}},
      columns/time/.style={column name={Time (s)}}] {benchmarks.data}
    \end{column}
  \end{columns}
 \end{frame}

\end{document}

最奇怪的是,条形图运行良好,但我无法正确编译表格。

欢迎任何帮助!

答案1

表格会自动假设输入的是数字,但您在paradigm列中有文本,因此需要string type指示符。您还可以通过后处理选项在单元格前添加/追加代码。

\documentclass[10pt]{beamer}
\usepackage{pgfplotstable,booktabs,colortbl,lmodern}
\pgfplotsset{compat=1.8}
\pgfplotstableread{
paradigm position time
{\shortstack{Procedural\\(append)}}     1       53
{\shortstack{Procedural\\(prepend)}}    2       64
{Object-Oriented}                       3       45
{Functional}                            4       44
{List Comprehension}                    5       30
}\mytable


\begin{document}

\begin{frame}
  \frametitle{Benchmark Results}
  \begin{columns}[m]
    \begin{column}{.5\textwidth}
      \begin{tikzpicture}[scale=.65]
        \begin{axis}[%
          ybar, bar width=0.65cm,%
          xmin=0.5,xmax=5.5, xtick=data,%
          xticklabels from table={\mytable}{paradigm},%
          xticklabel style={rotate=45,anchor=north east,inner sep=0mm},%
          ylabel={\Large Time (s)}, ylabel near ticks]
          \addplot table [x=position,y=time] {\mytable};
        \end{axis}
      \end{tikzpicture}      
    \end{column}
    \begin{column}{.5\textwidth}
      \pgfplotstabletypesetfile[%
      every even row/.style={before row={\rowcolor{blue!15}}},
      every head row/.style={before row=\toprule, after row=\midrule},
      every last row/.style={after row=\bottomrule},
      columns/paradigm/.style={
                     column name={Paradigm},
                     string type,
                     postproc cell content/.style={@cell content/.add={\scriptsize}{}}
                    },
      columns/position/.style={column name={Position}},
      columns/time/.style={column name={Time (s)}}] {\mytable}
    \end{column}
  \end{columns}
 \end{frame}

\end{document}

enter image description here

抱歉,内联数据。我有点懒得包含该filecontents包。

答案2

答案肯定是由 percusse 给出的,因此,如果您投票支持这个答案,也请投票支持percusse 的一个

这个答案只是为了提供完整的 LaTeX 代码(和最终渲染),以便收集我使用过的所有技巧(这可能对其他人有用)。

无论如何,如果您看到了可能的改进,请随时发表评论。

\documentclass[10pt]{beamer}

\usepackage{multirow, pgfplotstable,booktabs,colortbl,lmodern}

\pgfplotstableread{
paradigm        time2_7     time3_2
{Procedural (append)}   118.95      170.42
{Procedural (prepend)}  145.71      193.77
{Object-Oriented}        97.51      154.07
{Functional}             95.17      149.88
{List Comprehension}    106.26      162.37
}\mytable

\begin{document}

\begin{frame}
  \frametitle{Benchmark Results}

  \vfill
  \begin{columns}[m]
    \begin{column}{.5\textwidth}
      \begin{tikzpicture}[scale=.625]
        \begin{axis}[
          ybar, bar width=8pt,ymin=0,
          xmin=0.5,xmax=5.5, xtick=data,
          xticklabels from table={\mytable}{paradigm},
          xticklabel style={rotate=45,anchor=north east,inner sep=0mm},
          ylabel={\Large Time (s)}, ylabel near ticks]
          \addplot table [x expr=\coordindex+1,y=time2_7] {\mytable};
          \addlegendentry{Python 2.7};
          \addplot table [x expr=\coordindex+1,y=time3_2] {\mytable};
          \addlegendentry{Python 3.2};
        \end{axis}
      \end{tikzpicture}
    \end{column}

    \begin{column}{.6\textwidth}\scriptsize
      \pgfplotstabletypesetfile[
        columns={paradigm, time2_7, time3_2},
        every even row/.style={before row={\rowcolor{blue!15}}},
        every head row/.style={
          before row={\toprule 
            \multirow{2}{*}{\bfseries Programming Paradigm} & \multicolumn{2}{c}{\bfseries Time (s)}\\ \cmidrule{2-3}},
          after row=\midrule},
        every last row/.style={after row=\bottomrule},
        columns/paradigm/.style={
          column name={},
          string type},
        columns/time2_7/.style={
          column name={Python 2.7},
          postproc cell content/.style={@cell content/.add={}{\,s}}},
        columns/time3_2/.style={
          column name={Python 3.2},
          postproc cell content/.style={@cell content/.add={}{\,s}}}] 
          {\mytable}
    \end{column}
  \end{columns}
  \vfill

\end{frame}
\end{document}

最终结果如下:

enter image description here

相关内容