Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

I am trying to declare a new data format with the TikZ data visualization library for a custom data file format. The data files have a header section with Field name = Value followed by a blank line and subsequently the data in four columns.

My declared data format mydata seems to work, but I cannot figure out how to plot value 1 and value 2 as a function of time without reading the same data file twice. Any help appreciated.

\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}
share|improve this question
add comment

1 Answer 1

up vote 4 down vote accepted

One way to do this would be to create two \pgfdatapoints per line in the table, with the same variable names for x and y, but with a different set number:

\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}
share|improve this answer
    
Perfect, this works. Thanks! –  Martin Heller Apr 24 '13 at 6:53
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.