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.

In pgfplots is it possible to have several plots in one datafile?

The goal is to add about 20 plots, which has different number of points and not the same x-value, to the same axis. (app. 350 points)


One posible solution is to have the plots in separate column, and then use loads of 'nan' because of the different x values:

    x  p1   p2  P3  ...
    1  3    nan nan ...
    2  nan  5   nan ...
    6  4    8   nan ...
    8  7    nan nan ...
    ...

I do not like this solution:o(


The gnuplot solution is nice: the different plots are separated in the datafile by two newlines, and they are selected by 'index'. Does something similar exist in pgfplots? or any other solution?

share|improve this question
    
What program / method do you use to generate the datafiles? –  Jake Dec 8 '11 at 10:38
    
I've only ever tried this with one axis shared by all plots, so I'm not sure. Why do you want only one file though? –  qubyte Dec 8 '11 at 10:51
    
@MarkS.Everitt: I think Hans-Peter also wants the plots to be on the same axis, but they won't be defined at the same x-values. –  Jake Dec 8 '11 at 10:54
    
The data are collected from different scientific articles, so there are no generating program. I would like them plotted on the same axis. –  Hans-Peter E. Kristiansen Dec 8 '11 at 11:05
    
Oh, I see. Well that's easy then! Hang on... –  qubyte Dec 8 '11 at 11:09

2 Answers 2

up vote 4 down vote accepted

You can use gnuplot in the background by using \addplot gnuplot [raw gnuplot] {<gnuplot commands>};:

\documentclass{article}
\usepackage{pgfplots}
\usepackage{filecontents}

\begin{filecontents*}{data.txt}
1 1
2 4
3 9
4 16
5 25


1 1
2.5 5
6 12
\end{filecontents*}


\begin{document}
\begin{tikzpicture}
\begin{axis}
\addplot gnuplot [raw gnuplot] {plot 'data.txt' index 0};
\addplot gnuplot [raw gnuplot] {plot 'data.txt' index 1};
\end{axis}
\end{tikzpicture}
\end{document}
share|improve this answer
    
OK cool. I think that is the solution. I will try. The manual pretends that the gnuplot mode is only for function plotting:o) –  Hans-Peter E. Kristiansen Dec 8 '11 at 11:25
    
Is it described somewhere exactly how much gnuplot is in the background. Who is in control of point/line type/color, .... –  Hans-Peter E. Kristiansen Dec 8 '11 at 11:35
    
It's all PGFplots, all gnuplot does is generate a data file that is then read in PGFplots using \addplot file .... From the PGFplots manual: "In contrast to plot expression, the plot gnuplot command employs the external program gnuplot to compute coordinates. The resulting coordinates are written to a text file which will be plotted with plot file". –  Jake Dec 8 '11 at 11:37
    
Ok thank you. I knew that was the case with a function passed to gnuplot, but of cause its the same here. –  Hans-Peter E. Kristiansen Dec 8 '11 at 11:49

You can use the \addplot command multiple times on the same axes. The files that the different \addplots take can be the same or different, and different columns etc.

\pgfplotsset{width=6cm,compat=newest}
\begin{axis}[
    scale only axis,
    xlabel={$\tau$ ($\mu$s)},
    ylabel={P(bitflip)},
    ymin=0,
    ymax=1,
    xmin=0,
    xmax=125.66,
]
\addplot[color=red]
    table[x=time,y=fidelity] {data1.txt};
\addplot[color=black]
    table[x=time,y=fidelity] {data1.txt};
\end{axis}
\end{tikzpicture}

It's not so clear here, but I used data from two sample files to plot this

enter image description here

The data files here are indexed by column headers, but you can also do this by number. One of the files begins with the following lines:

time    fidelity
0.00000000e+00  0.00000000e+00
2.02874597e-01  9.75413405e-05
5.19896845e-01  4.76482130e-04
8.36998426e-01  7.26895359e-04
1.15410184e+00  7.52986468e-04
1.47115744e+00  8.85559658e-04

You can put multiple columns in one file, and gaps (just whitespace) are tolerated. In the following plot one data file is used with four columns, and there are clear gaps in the time and amplitude entries for the red plot.

enter image description here

\pgfplotsset{width=6cm,compat=newest}
\begin{tikzpicture}
\begin{axis}[
    scale only axis,
    xlabel={$\tau$ (ns)},
    ylabel={Negativity},
    ymin=-1,
    ymax=1,
    xmin=0,
    xmax=6.3,
    legend style={font=\small,at={(0.5,0.96)},anchor=north,style={nodes={right}}},
]
\addplot[color=black]
    table[x index=0,y index=1] {data.txt};
\addplot[color=red]
    table[x index=2,y index=3] {data.txt};
\end{axis}
\end{tikzpicture}

Note that there are no column headers this time (just for variety). Gaps look like

2.28479466e+00  7.55749574e-01  -6.54860734e-01 2.28479466e+00  
2.34826118e+00  7.12694171e-01  -7.01474888e-01 2.34826118e+00  
2.41172769e+00  6.66769001e-01  -7.45264450e-01 2.41172769e+00  
2.47519421e+00  6.18158986e-01      
2.53866073e+00  5.67059864e-01      
2.60212725e+00  5.13677392e-01      
2.66559377e+00  4.58226522e-01      
2.72906028e+00  4.00930535e-01      
2.79252680e+00  3.42020143e-01      
2.85599332e+00  2.81732557e-01      
2.91945984e+00  2.20310533e-01      
2.98292636e+00  1.58001396e-01      
3.04639288e+00  9.50560433e-02      
3.10985939e+00  3.17279335e-02      
3.17332591e+00  -3.17279335e-02     
3.23679243e+00  -9.50560433e-02     
3.30025895e+00  -1.58001396e-01     
3.36372547e+00  -2.20310533e-01     
3.42719199e+00  -2.81732557e-01     
3.49065850e+00  -3.42020143e-01     
3.55412502e+00  -4.00930535e-01     
3.61759154e+00  -4.58226522e-01 -8.88835449e-01 3.61759154e+00  
3.68105806e+00  -5.13677392e-01 -8.57983413e-01 3.68105806e+00  
3.74452458e+00  -5.67059864e-01 -8.23676581e-01 3.74452458e+00  
3.80799110e+00  -6.18158986e-01 -7.86053095e-01 3.80799110e+00

It should also be fine to have uneven columns, provided the number of tabs or commas is correct.

share|improve this answer
    
Yes - that was my idea, but it involves loads of loads of 'nan' because of the different x-values. -or maybe I do not understand. –  Hans-Peter E. Kristiansen Dec 8 '11 at 11:21
    
But wouldn't this solution require one datafile per plot? If I understand correctly, Hans-Peter wants to be able to generate several different plots (with different number of data points and different sampling intervals) from a single data.csv file. –  Jake Dec 8 '11 at 11:22
    
Nope. See my update. pgfplots doesn't care about missed columns, so long as the number of tabs or commas is correct. At least that's what my basic trials showed. Not sure if it'll stand up to too much abuse. This is similarly true of uneven column lengths. –  qubyte Dec 8 '11 at 11:50
    
Oh yeah, okay, but the problem that you have to provide the right number of somethings, be they nan or tabs or commas, would remain with this approach. –  Jake Dec 8 '11 at 12:03
    
@Jake: Then the answer is simple. Place the columns in descending order of length, or script a solution to add in the extra tabs or columns. That should not be difficult. –  qubyte Dec 8 '11 at 12:06

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.