如何使用 TeX 从 CSV 绘制图形?

如何使用 TeX 从 CSV 绘制图形?

我是 TeX 的完全初学者,我想使用 TeX 绘制 JMH 基准图。因为我有 CSV 格式的输出结果。这是我的实际代码:

\documentclass{article}
\usepackage{pgfplots}
\usepackage{pgfplotstable}
\usepackage{siunitx}
\usepackage{graphicx}
\pgfplotsset{compat=1.5}
\title{Performance Report}
\date{}

\begin{document}

\maketitle

\begin{tikzpicture}
\begin{axis}[title=Getbytes performance,
xlabel = String length,
ylabel = Performance op/s,
 ]

\addplot[
only marks,
mark = *,
      /pgfplots/error bars/.cd,
    x dir = both,
    y dir = both,
    x explicit,
    y explicit,
   ]
table [
x=Param: length,
y=Score,
 ]
{jmh-result-getbytes.csv};

\addlegendentry{Average value}
\end{axis}
\end{tikzpicture}

\end{document}

我的 CSV 文件:

"Benchmark","Mode","Threads","Samples","Score","Score Error (99.9%)","Unit","Param: encoding","Param: length"
"test","thrpt",1,25.000000,24275.400025,695.792149,"ops/ms",ISO-8859-1,1
"test","thrpt",1,25.000000,23954.805784,545.186364,"ops/ms",ISO-8859-1,2
"test","thrpt",1,25.000000,23205.982297,573.971327,"ops/ms",ISO-8859-1,4
"test","thrpt",1,25.000000,24272.662601,937.668790,"ops/ms",ISO-8859-1,8
"test","thrpt",1,25.000000,24324.370910,1122.289177,"ops/ms",ISO-8859-1,16
"test","thrpt",1,25.000000,23960.521519,928.378626,"ops/ms",ISO-8859-1,32

对我来说 x 是:Param: length 而 y 是:score

但是当我运行时,make plot.pdf出现以下消息错误:

! Package pgfplots Error: Sorry, could not retrieve column 'Param:     length' from
 table '\\pgfplotstableread@filename@@table@name '. Please check spelling (or introduce name aliases)..

答案1

列标题中的百分号字符"Score Error (99.9%)"会带来麻烦,因为它会删除行的其余部分作为注释。以下示例通过忽略此字符来解决此问题。

此外,标题条目被引号包围。 似乎不支持引用pgfplots。因此,示例将引号作为标题的一部分添加到列标题规范中。此外,您必须祈祷单元格不包含逗号。否则,pgfplots即使逗号在引号内,也会认为新列已开始。

\documentclass{article}
\usepackage{pgfplots}
\usepackage{pgfplotstable}
\usepackage{siunitx}
\usepackage{graphicx}
\pgfplotsset{compat=1.5}
\title{Performance Report}
\date{}

\begin{document}

\maketitle

\begin{tikzpicture}
\begin{axis}[title=Getbytes performance,
    xlabel = Performance op/s,
    ylabel = String length,
 ]

\addplot[
    only marks,
    mark = *,
    /pgfplots/error bars/.cd,
    x dir = both,
    y dir = both,
    x explicit,
    y explicit,
   ]
table [
    header=true,
    col sep=comma,
    ignore chars=\%,
    x="Param: length",
    y="Score",
 ]
{jmh-result-getbytes.csv};

\addlegendentry{Average value}
\end{axis}
\end{tikzpicture}

\end{document}

结果

相关内容