如何为 pgfplots 自动转义任何 CSV 文件

如何为 pgfplots 自动转义任何 CSV 文件

我想从文件“processor.csv”中绘制一些内容http://cpudb.stanford.edu/cpudb.1416196069.zip

我如何才能自动转义此文件或任何其他文件以将其与 pgfplots 一起使用。我已经尝试使用查找和替换等手动编辑它。但我仍然收到错误,在我看来,这不应该是解决问题的方法。

无论我想要使用哪个 CSV 文件,是否都没有自动方法可以转义所有需要转义的内容?

这是我尝试绘制的方式

\begin{figure}
\begin{tikzpicture}
\begin{axis}
\addplot table [x=clock, y=created_at, col sep=comma] {./processor.csv};
\end{axis}
\end{tikzpicture}
\end{figure}

答案1

pgfplots 的 CSV 读取功能缺乏转义支持。因此,短的答案是:没有办法(取消)逃避随意的CSV 列pgfplots,抱歉。

也没有内置支持datetimes以所需的精度进行绘图(您的数据文件以十分之一秒或毫秒为单位)。

您的评论表明您正在寻找一种功能齐全、功能强大的解决方案,该解决方案适用于所有情况。在这种情况下,您可能需要评估其他工具。


话虽如此,还是有办法得到你想要的图。这超出了 pgfplots 的内置功能,并且对于所有输入类型都不稳定,所以要小心。

关于如何读取数据文件的问题:pgfplots支持默默忽略输入文件中的字符。对于你的情况,这足以读取文件。方法如下

\addplot table [x=clock, y=created_at, col sep=comma, ignore chars={\#,\"}] {./processor.csv};

在这种情况下,任何出现的"#都会被悄悄地删除。这实际上读取了附加文件的所有记录。

幸运的是,该数据文件的字符串列内不包含任何,字符(当然,这纯属运气 - 只要数据集包含逗号,它就会失败)。

如上所述,pgfplots 还有另一个限制:您的数据文件在其中一个轴上使用日期时间,并且提供的数据文件以数十秒为单位。但是,pgfplots 必须使用 TeX 提供的有限算法(最多 32 位整数),并且最多支持分钟级的日期时间范围。

虽然可以通过适当的变换来调整(例如pgfplots 数据时间格式),它不是一个“开箱即用”的解决方案。以下是支持时间的建议采用(但忽略日期部分)。 它摘自上面@Jake的回答:

\documentclass{standalone}

\usepackage{pgfplots}

\pgfplotsset{compat=1.12}

\usepgfplotslibrary{dateplot}

% this strips the date (silently!) and considers only the time
\def\transformtime#1 #2:#3:#4.#5!{
    \pgfkeys{/pgf/fpu=true,/pgf/fpu/output format=fixed}%
    \count2=#2 \edef\hour{\the\count2}%
    \count2=#3 \edef\min{\the\count2}%
    \count2=#4 \edef\sec{\the\count2}%
    \pgfmathparse{\hour*3600-\pgfkeysvalueof{/pgfplots/timeplot zero}*3600+\min*60+\sec + 0.#5}%
    \pgfkeys{/pgf/fpu=false}%
    %\message{got #1 #2:#3:#4.#5 -> \pgfmathresult^^J}%
}

\pgfplotsset{
    timeplot zero/.initial=0,
    timeplot/.style={
        y coord trafo/.code={\expandafter\transformtime##1!},
        y coord inv trafo/.code={%
            \pgfkeys{/pgf/fpu=true,/pgf/fpu/output format=fixed}
            \pgfmathsetmacro\hours{floor(##1/3600)+\pgfkeysvalueof{/pgfplots/timeplot zero}}
            \pgfmathsetmacro\minutes{floor((##1-(\hours-\pgfkeysvalueof{/pgfplots/timeplot zero})*3600)/60)}
            \pgfmathsetmacro\seconds{##1-floor(##1/60)*60}
            \def\pgfmathresult{\pgfmathprintnumber{\hours}:\pgfmathprintnumber{\minutes}:\pgfmathprintnumber[fixed zerofill]{\seconds}}
            \pgfkeys{/pgf/fpu=false}
        },
    scaled y ticks=false,
    yticklabel=\tick,
    }
}

\begin{document}

\begin{tikzpicture}
    \begin{axis}[
        xmax=1000,
        timeplot,
    ]
    \addplot+[only marks]
        table [x=clock, y=created_at, col sep=comma, ignore chars={\#,\"}] {./processor.csv};
    \end{axis}
\end{tikzpicture}
\end{document}

在此处输入图片描述

限制:

  1. CSV 文件中的转义逗号将破坏解析程序
  2. 数据点必须在一天内,此宏中没有健全性检查
  3. 您的数据文件包含几个没有 x 坐标的数据点,这些数据点将被删除

用于重现此组的数据文件是

id,created_at,updated_at,manufacturer_id,processor_family_id,microarchitecture_id,code_name_id,technology_id,cache_on_id,cache_off_id,die_photo_id,model,date,clock,max_clock,hw_nthreadspercore,hw_ncores,tdp,source,bus_width,transistors,die_size,vdd_low,vdd_high
1,"2014-11-17 00:01:26.708453","2014-11-17 00:01:26.708453",9,1,,1,,1,2,,3.60E,,3600,,1,1,110,http://ark.intel.com/products/27089/64-bit-Intel-Xeon-Processor-3_60E-GHz-2M-Cache-800-MHz-FSB,,169,135,1.2875,1.3875
2,"2014-11-17 00:01:26.732099","2014-11-17 00:01:26.732099",9,1,,1,,1,2,,3.6,,3600,,2,1,110,http://ark.intel.com/products/28019/64-bit-Intel-Xeon-Processor-3_60-GHz-2M-Cache-800-MHz-FSB,,169,135,1.25,1.388
3,"2014-11-17 00:01:26.754340","2014-11-17 00:01:26.754340",9,1,,1,,1,2,,3.80E,,3800,,1,1,110,http://ark.intel.com/products/27092/64-bit-Intel-Xeon-Processor-3_80E-GHz-2M-Cache-800-MHz-FSB,,169,135,1.2875,1.3875
4,"2014-11-17 00:01:26.796928","2014-11-17 00:01:26.796928",9,1,,2,,3,2,,,,3660,,2,1,110,http://ark.intel.com/products/27103/64-bit-Intel-Xeon-Processor-3_66-GHz-1M-Cache-667-MHz-FSB,,125,112,1.2875,1.4
5,"2014-11-17 00:01:26.847382","2014-11-17 00:01:26.847382",9,2,,3,,4,2,,310,,2130,,1,1,73,http://ark.intel.com/products/27104/Intel-Celeron-D-Processor-310-(256K-Cache-2_13-GHz-533-MHz-FSB),,125,112,1.25,1.4
6,"2014-11-17 00:01:26.868530","2014-11-17 00:01:26.868530",9,2,,3,,4,2,,315,,2260,,1,1,73,http://ark.intel.com/products/27105/Intel-Celeron-D-Processor-315-(256K-Cache-2_26-GHz-533-MHz-FSB),,125,112,1.25,1.4
7,"2014-11-17 00:01:26.888672","2014-11-17 00:01:26.888672",9,2,,3,,4,2,,315J,,2260,,1,1,73,http://ark.intel.com/products/27106/Intel-Celeron-D-Processor-315315J-(256K-Cache-2_26-GHz-533-MHz-FSB),,125,112,1.25,1.4
8,"2014-11-17 00:01:26.908313","2014-11-17 00:01:26.908313",9,2,,3,,4,2,,320,,2400,,1,1,73,http://ark.intel.com/products/27107/Intel-Celeron-D-Processor-320-(256K-Cache-2_40-GHz-533-MHz-FSB),,125,112,1.25,1.4
9,"2014-11-17 00:01:26.929046","2014-11-17 00:01:26.929046",9,2,,3,,4,2,,325,,2530,,1,1,73,http://ark.intel.com/products/27108/Intel-Celeron-D-Processor-325-(256K-Cache-2_53-GHz-533-MHz-FSB),,125,112,1.25,1.4
10,"2014-11-17 00:01:26.951261","2014-11-17 00:01:26.951261",9,2,,3,,4,2,,325J,,2530,,1,1,84,http://ark.intel.com/products/27110/Intel-Celeron-D-Processor-325J-(256K-Cache-2_53-GHz-533-MHz-FSB),,125,112,1.25,1.4
11,"2014-11-17 00:01:26.974090","2014-11-17 00:01:26.974090",9,2,,3,,4,2,,326,,2530,,1,1,84,http://ark.intel.com/products/27111/Intel-Celeron-D-Processor-326-(256K-Cache-2_53-GHz-533-MHz-FSB),,125,112,1.25,1.4
12,"2014-11-17 00:01:26.995734","2014-11-17 00:01:26.995734",9,2,,3,,4,2,,330,,2660,,1,1,73,http://ark.intel.com/products/27112/Intel-Celeron-D-Processor-330-(256K-Cache-2_66-GHz-533-MHz-FSB),,125,112,1.25,1.4
13,"2014-11-17 00:01:27.018634","2014-11-17 00:01:27.018634",9,2,,3,,4,2,,330J,,2660,,1,1,84,http://ark.intel.com/products/27114/Intel-Celeron-D-Processor-330J-(256K-Cache-2_66-GHz-533-MHz-FSB),,125,112,1.25,1.4
14,"2014-11-17 00:01:27.039080","2014-11-17 00:01:27.039080",9,2,,3,,4,2,,331,,2660,,1,1,84,http://ark.intel.com/products/27115/Intel-Celeron-D-Processor-331-(256K-Cache-2_66-GHz-533-MHz-FSB),,125,112,1.25,1.4
15,"2014-11-17 00:01:27.062386","2014-11-17 00:01:27.062386",9,2,,3,,4,2,,335J,,2800,,1,1,84,http://ark.intel.com/products/27118/Intel-Celeron-D-Processor-335J-(256K-Cache-2_80-GHz-533-MHz-FSB),,125,112,1.25,1.4
16,"2014-11-17 00:01:27.142737","2014-11-17 00:01:27.142737",9,2,,3,,4,2,,336,,2800,,1,1,84,http://ark.intel.com/products/27119/Intel-Celeron-D-Processor-336-(256K-Cache-2_80-GHz-533-MHz-FSB),,125,112,1.25,1.4
17,"2014-11-17 00:01:27.165279","2014-11-17 00:01:27.165279",9,2,,3,,4,2,,340,,2930,,1,1,73,http://ark.intel.com/products/27120/Intel-Celeron-D-Processor-340-(256K-Cache-2_93-GHz-533-MHz-FSB),,125,112,1.25,1.4
18,"2014-11-17 00:01:27.188150","2014-11-17 00:01:27.188150",9,2,,3,,4,2,,340J,,2930,,1,1,84,http://ark.intel.com/products/27121/Intel-Celeron-D-Processor-340J-(256K-Cache-2_93-GHz-533-MHz-FSB),,125,112,1.25,1.4
19,"2014-11-17 00:01:27.208418","2014-11-17 00:01:27.208418",9,2,,3,,4,2,,345,,3060,,1,1,73,http://ark.intel.com/products/27123/Intel-Celeron-D-Processor-345-(256K-Cache-3_06-GHz-533-MHz-FSB),,125,112,1.25,1.4
20,"2014-11-17 00:01:27.228689","2014-11-17 00:01:27.228689",9,2,,3,,4,2,,345J,,3060,,1,1,84,http://ark.intel.com/products/27124/Intel-Celeron-D-Processor-345J-(256K-Cache-3_06-GHz-533-MHz-FSB),,125,112,1.25,1.4
21,"2014-11-17 00:01:27.251224","2014-11-17 00:01:27.251224",9,2,,3,,4,2,,346,,3060,,1,1,84,http://ark.intel.com/products/27125/Intel-Celeron-D-Processor-346-(256K-Cache-3_06-GHz-533-MHz-FSB),,125,112,1.25,1.4
22,"2014-11-17 00:01:27.272903","2014-11-17 00:01:27.272903",9,2,,3,,4,2,,350,,3200,,1,1,73,http://ark.intel.com/products/27126/Intel-Celeron-D-Processor-350-(256K-Cache-3_20-GHz-533-MHz-FSB),,125,112,1.25,1.4
23,"2014-11-17 00:01:27.293688","2014-11-17 00:01:27.293688",9,2,,3,,4,2,,350J,,3200,,1,1,73,http://ark.intel.com/products/27127/Intel-Celeron-D-Processor-350350J-(256K-Cache-3_20-GHz-533-MHz-FSB),,125,112,1.25,1.4
24,"2014-11-17 00:01:27.316604","2014-11-17 00:01:27.316604",9,2,,3,,4,2,,351,,3200,,1,1,84,http://ark.intel.com/products/27128/Intel-Celeron-D-Processor-351-(256K-Cache-3_20-GHz-533-MHz-FSB),,125,112,1.25,1.4
25,"2014-11-17 00:01:27.338452","2014-11-17 00:01:27.338452",9,2,,3,,4,2,,355,,3330,,1,1,84,http://ark.intel.com/products/27130/Intel-Celeron-D-Processor-355-(256K-Cache-3_33-GHz-533-MHz-FSB),,125,112,1.25,1.4
26,"2014-11-17 00:01:27.381180","2014-11-17 00:01:27.381180",9,2,,4,,5,2,,356,,3330,,1,1,86,http://ark.intel.com/products/27131/Intel-Celeron-D-Processor-356-(512K-Cache-3_33-GHz-533-MHz-FSB),,188,81,1.25,1.325
27,"2014-11-17 00:01:27.400815","2014-11-17 00:01:27.400815",9,2,,4,,5,2,,360,,3460,,1,1,65,http://ark.intel.com/products/27132/Intel-Celeron-D-Processor-360-(512K-Cache-3_46-GHz-533-MHz-FSB),,188,81,1.25,1.325
28,"2014-11-17 00:01:27.442228","2014-11-17 00:01:27.442228",9,3,,5,,5,2,,310,,1200,,1,1,24.5,http://ark.intel.com/products/27138/Intel-Celeron-M-Processor-310-(512K-Cache-1_20-GHz-400-MHz-FSB),,77,83,,1.356
29,"2014-11-17 00:01:27.462385","2014-11-17 00:01:27.462385",9,3,,5,,5,2,,330,,1400,,1,1,24.5,http://ark.intel.com/products/27140/Intel-Celeron-M-Processor-330-(512K-Cache-1_40-GHz-400-MHz-FSB),,77,83,,1.356
30,"2014-11-17 00:01:27.484997","2014-11-17 00:01:27.484997",9,3,,5,,5,2,,340,,1500,,1,1,24.5,http://ark.intel.com/products/27141/Intel-Celeron-M-Processor-340-(512K-Cache-1_50-GHz-400-MHz-FSB),,77,83,,1.356
31,"2014-11-17 00:01:27.518347","2014-11-17 00:01:27.518347",9,3,,6,,3,2,,350,,1300,,1,1,21,http://ark.intel.com/products/27142/Intel-Celeron-M-Processor-350-(1M-Cache-1_30-GHz-400-MHz-FSB),,144,87,,1.26
32,"2014-11-17 00:01:27.538306","2014-11-17 00:01:27.538306",9,3,,6,,3,2,,360,,1400,,1,1,21,http://ark.intel.com/products/27143/Intel-Celeron-M-Processor-360-(1M-Cache-1_40-GHz-400-MHz-FSB),,144,87,,
33,"2014-11-17 00:01:27.558462","2014-11-17 00:01:27.558462",9,3,,6,,3,2,,380,,1600,,1,1,21,http://ark.intel.com/products/27146/Intel-Celeron-M-Processor-380-(1M-Cache-1_60-GHz-400-MHz-FSB),,144,87,1.004,1.292
34,"2014-11-17 00:01:27.580314","2014-11-17 00:01:27.580314",9,3,,6,,3,2,,390,,1700,,1,1,21,http://ark.intel.com/products/27147/Intel-Celeron-M-Processor-390-(1M-Cache-1_70-GHz-400-MHz-FSB),,144,87,1.004,1.292
35,"2014-11-17 00:01:27.611632","2014-11-17 00:01:27.611632",9,3,,7,,3,2,,410,,1460,,1,1,27,http://ark.intel.com/products/27148/Intel-Celeron-M-Processor-410-(1M-Cache-1_46-GHz-533-MHz-FSB),,151,90,1,1.3
36,"2014-11-17 00:01:27.632430","2014-11-17 00:01:27.632430",9,3,,7,,3,2,,420,,1600,,1,1,27,http://ark.intel.com/products/27149/Intel-Celeron-M-Processor-420-(1M-Cache-1_60-GHz-533-MHz-FSB),,151,90,1,1.3
37,"2014-11-17 00:01:27.656233","2014-11-17 00:01:27.656233",9,3,,7,,3,2,,430,,1730,,1,1,27,http://ark.intel.com/products/27150/Intel-Celeron-M-Processor-430-(1M-Cache-1_73-GHz-533-MHz-FSB),,151,90,1,1.3
38,"2014-11-17 00:01:27.676503","2014-11-17 00:01:27.676503",9,3,,7,,3,2,,450,,2000,,1,1,27,http://ark.intel.com/products/27152/Intel-Celeron-M-Processor-450-(1M-Cache-2_00-GHz-533-MHz-FSB),,151,90,1,1.3
39,"2014-11-17 00:01:27.695606","2014-11-17 00:01:27.695606",9,3,,5,,5,2,,333,,900,,1,1,7,http://ark.intel.com/products/27156/Intel-Celeron-M-Processor-ULV-333-(512K-Cache-900-MHz-400-MHz-FSB),,77,83,,1.004

以及数千行此类内容

相关内容