0

I need to plot all the text files I have in a directory simultaneously. I plot them individually with gnuplot and it worked fine. So my data is ok. But since there is too many of files I'm looking for a script to do it for me.

I tried to follow the instruction here, so typed this in the gnuplot

a=system('a=`tempfile`;cat *.txt > $a;echo "$a"')
plot a u 3:2

but I get this error:

         plot a u 3:2
                    ^
         warning: Skipping data file with no valid points
                     ^
         x range is invalid

Ady idea? I'm new to gnuplot, so I don't really understand what does plot a u 3:2 is supposed to do.

Community
  • 1
  • 1
samira
  • 399
  • 1
  • 3
  • 12
  • Are your txt files on the same path as your script? – Atirag Feb 04 '16 at 00:22
  • you were right, as soon as I noticed I fixed it and updated the question with the error. – samira Feb 04 '16 at 00:31
  • I'm nt familiar with gnuplot. What does u 3:2 mean? maybe there's some mismatch with the actual data and the plot configuration. – Atirag Feb 04 '16 at 00:36
  • Show us your data: `cat *.txt` (in the system). Show us `a`: `a` (in `gnuplot`). This would already (probably) disclose you a problem. `u 3:2` means plot `3rd` column against `2nd` column. You do not have such columns, and do not have a plot - easy ;) – John_West Feb 04 '16 at 00:38
  • :) `u` is the short name for `using`. (Other abbreviations, [see 4.1 here](http://people.duke.edu/~hpgavin/gnuplot.html)) – John_West Feb 04 '16 at 00:49
  • Thanks. I used 'plot a u 1:2' and it fixed. – samira Feb 04 '16 at 16:09

1 Answers1

0

u 3:2 means plot 3rd column against 2nd column.

You do not have such columns, and do not have a plot - easy.

Just misuse of using.


Update:
How to plot each file with different color, and what the line `system(...)` means

Plot, modifying the solution you initially found

a=system('a=`tempfile`; awk "{if ((FNR==1) && (NR!=1)) {printf \"\n\n\"}; print \$0}" *.txt > $a;echo $a;')
plot a u 1:2:(column(-2)) with lines lc variable

Explanation:

system makes Unix system call to execute some command (string) in shell.

String is placed in single or double quotes inside parenthesis: system('...').

tempfile is a Linux utility to create temp file.

To plot files with different colors one could use lc variable option gnuplot. It needs the data to be separated into blocks. We use two blank lines to separate blocks.

awk could process multiple files: awk '...' list_of_files..., I use double quotes awk "<command>" to distinguish between these quotes and quotes from system command.

awk "{if ((FNR==1) && (NR!=1)) {printf \"\n\n\"}; print \$0}" *.txt

If we have the first line of some file (FNR==1), but not the first file (total number of records processed NR!=1), we should print two blank lines printf \"\n\n\" to indent new block for gnuplot.

print \$0 prints full line ($0). Double quote \" before newline symbol \n and $ are escaped to be not expanded by the shell - the drawback of " " (even, it is enough to tell awk: print).

Example:

==1.txt==
1 1
2 2
3 3
==2.txt==
4 1
5 2
6 3
==3.txt==
7 0
7 2
8 4
8 2

Output from awk command (see the command above):

1 1
2 2
3 3


4 1
5 2
6 3


7 0
7 2
8 4
8 2

plot a u 1:2:(column(-2)) with lines lc variable makes gnuplot plot the 2nd column vs the 1st one, coloring each block of data with its index. Index of data block is obtained with special gnuplot variable: (column(-2)).

John_West
  • 2,239
  • 4
  • 24
  • 44
  • hanks. I used 'plot a u 1:2' and it fixed. Now I have a plot with hundreds of graphs in it. Do You know how can I make them distinguishable? probably with different colors or more resolution? I also don't know what 'a=system('a=`tempfile`;cat *.txt > $a;echo "$a"')' exactly do? – samira Feb 04 '16 at 16:11
  • Oh, it is not for two words - I will update the answer then! – John_West Feb 05 '16 at 00:49