我正在尝试使用 TikZ 数据可视化库为自定义数据文件格式声明一种新的数据格式。数据文件有一个标题部分,后面Field name = Value
跟着一个空白行,然后是四列数据。
我声明的数据格式mydata
似乎有效,但我无法弄清楚如何在不读取同一数据文件两次的情况下绘制value 1
和value 2
作为的函数。任何帮助都感激不尽。time
\documentclass{article}
\usepackage{tikz}
\usepackage{filecontents}
\begin{filecontents*}{"datafile.txt"}
Header field 1 = Header value 1
Header field 2 = Header value 2
Header field 3 = ...
Header field N = Header value N
Data
0 -6.283 0.000 1.000
1 -5.027 0.951 0.309
2 -3.770 0.588 -0.809
3 -2.513 -0.588 -0.809
4 -1.257 -0.951 0.309
5 0.000 0.000 1.000
6 1.257 0.951 0.309
7 2.513 0.588 -0.809
8 3.770 -0.588 -0.809
9 5.027 -0.951 0.309
10 6.283 0.000 1.000
\end{filecontents*}
\usetikzlibrary{datavisualization}
\def\ignoreheader{%
\catcode`A=14\relax\catcode`B=14\relax\catcode`C=14\relax\catcode`D=14\relax
\catcode`E=14\relax\catcode`F=14\relax\catcode`G=14\relax\catcode`H=14\relax
\catcode`I=14\relax\catcode`J=14\relax\catcode`K=14\relax\catcode`L=14\relax
\catcode`M=14\relax\catcode`N=14\relax\catcode`O=14\relax\catcode`P=14\relax
\catcode`Q=14\relax\catcode`R=14\relax\catcode`S=14\relax\catcode`T=14\relax
\catcode`U=14\relax\catcode`V=14\relax\catcode`X=14\relax\catcode`Y=14\relax
\catcode`Z=14\relax
}%
\pgfdeclaredataformat{mydata}%
{\ignoreheader}{}{#1 #2 #3 #4}
{%
\pgfkeyssetvalue{/data point/index}{#1}%
\pgfkeyssetvalue{/data point/time}{#2}%
\pgfkeyssetvalue{/data point/value 1}{#3}%
\pgfkeyssetvalue{/data point/value 2}{#4}%
\pgfdatapoint
}{}{}%
\pgfdeclaredataformat{value 1}%
{\ignoreheader}{}{#1 #2 #3 #4}
{%
\pgfkeyssetvalue{/data point/time}{#2}%
\pgfkeyssetvalue{/data point/value}{#3}%
\pgfdatapoint
}{}{}%
\pgfdeclaredataformat{value 2}%
{\ignoreheader}{}{#1 #2 #3 #4}
{%
\pgfkeyssetvalue{/data point/time}{#2}%
\pgfkeyssetvalue{/data point/value}{#4}%
\pgfdatapoint
}{}{}%
\begin{document}
This works, but the is read twice\\
\begin{tikzpicture}
\datavisualization[%
school book axes,
visualize as line/.list={value 1,value 2},
x axis={length=10cm,attribute=time},
y axis={length=3cm,attribute=value},
style sheet=strong colors,
]
data[set=value 1,read from file={"datafile.txt"},format=value 1]
data[set=value 2,read from file={"datafile.txt"},format=value 2];
\end{tikzpicture}
Plotting each of values 1 and 2 in a separate visualization works:\\
\begin{tikzpicture}
\datavisualization[%
school book axes,
visualize as line,
x axis={length=10cm,attribute=time},
y axis={length=3cm,attribute=value 1},
style sheet=strong colors,
]
data[read from file={"datafile.txt"},format=mydata];
\end{tikzpicture}
\begin{tikzpicture}
\datavisualization[%
school book axes,
visualize as line,
x axis={length=10cm,attribute=time},
y axis={length=3cm,attribute=value 2},
style sheet=strong colors,
]
data[read from file={"datafile.txt"},format=mydata];
\end{tikzpicture}
This does not work:\\
\begin{tikzpicture}
\datavisualization[%
school book axes,
visualize as line/.list={value 1,value 2},
x axis={length=10cm,attribute=time},
y axis={length=3cm,attribute/.list={value 1,value 2}},
style sheet=strong colors,
]
data[read from file={"datafile.txt"},format=mydata];
\end{tikzpicture}
\end{document}
答案1
实现此目的的一种方法是,\pgfdatapoint
在表中每行创建两个 s,和 具有相同的变量名x
,y
但具有不同的set
编号:
\documentclass{article}
\usepackage{tikz}
\usepackage{filecontents}
\begin{filecontents*}{"datafile.txt"}
Header field 1 = Header value 1
Header field 2 = Header value 2
Header field 3 = ...
Header field N = Header value N
Data
0 -6.283 0.000 1.000
1 -5.027 0.951 0.309
2 -3.770 0.588 -0.809
3 -2.513 -0.588 -0.809
4 -1.257 -0.951 0.309
5 0.000 0.000 1.000
6 1.257 0.951 0.309
7 2.513 0.588 -0.809
8 3.770 -0.588 -0.809
9 5.027 -0.951 0.309
10 6.283 0.000 1.000
\end{filecontents*}
\usetikzlibrary{datavisualization}
\def\ignoreheader{%
\catcode`A=14\relax\catcode`B=14\relax\catcode`C=14\relax\catcode`D=14\relax
\catcode`E=14\relax\catcode`F=14\relax\catcode`G=14\relax\catcode`H=14\relax
\catcode`I=14\relax\catcode`J=14\relax\catcode`K=14\relax\catcode`L=14\relax
\catcode`M=14\relax\catcode`N=14\relax\catcode`O=14\relax\catcode`P=14\relax
\catcode`Q=14\relax\catcode`R=14\relax\catcode`S=14\relax\catcode`T=14\relax
\catcode`U=14\relax\catcode`V=14\relax\catcode`X=14\relax\catcode`Y=14\relax
\catcode`Z=14\relax
}%
\pgfdeclaredataformat{mydata}%
{\ignoreheader}{}{#1 #2 #3 #4}
{%
\pgfkeys{/data point/.cd,index=#1, x=#2, y=#3, set=1} \pgfdatapoint
\pgfkeys{/data point/.cd,index=#1, x=#2, y=#4, set=2} \pgfdatapoint
}{}{}%
\begin{document}
\begin{tikzpicture}
\datavisualization[%
school book axes,
visualize as line/.list={1,2},
style sheet=strong colors
]
data [read from file="datafile.txt", format=mydata];
\end{tikzpicture}
\end{document}