我正在使用 创建许多非常相似的图pgfplots
。这些图的原始数据在一个文件中提供.csv
,因此类似于
x,a,b,c,d,e
0,0.04,0.03,0.04,0.03,0.04
1,0.06,0.05,0.06,0.05,0.06
2,0.06,0.07,0.06,0.07,0.06
3,0.09,0.08,0.09,0.08,0.09
4,0.13,0.12,0.13,0.12,0.13
5,0.13,0.12,0.13,0.12,0.13
(在真实情况下,行和列要多得多。)每个图都是独立的(IE不在同一轴上),但它们是使用包含以下形式的绘图环境的合适宏“半自动”生成的
\newcommand{\buildplot}[1]%
{%
\begin{tikzpicture}
\begin{axis}
[
axis x line = bottom,
axis y line = left,
]
\addplot [mark = none]
table [col sep = comma, y index = #1]
{example.csv};
\end{axis}
\end{tikzpicture}
}
图表需要区分,最简单的方法是使用列标题(在示例数据中a
,b
ETC。) 作为title
密钥。但是,我无法找到在绘图环境中恢复标题的方法来实现这一点。由于信息已经在中.csv
,如果可能的话,我宁愿避免在文档中重新输入它。
答案1
以下是部分解决方案,展示了您可以如何处理它。缺点是它使用write18
。示例使用head -1
从文件中提取第一行.csv
,然后使用另一个程序处理输出,将其保存到文件并包含该文件。为了示例的目的,我使用它sed
来更改分隔符。您可以随意将其更改为您想要的任何内容。
\immediate\write18{head -1 data.csv | sed -e 's/,/ delim /g' > file.tex}
\input{file.tex}