1

I have created stacked graphs which provide x axis are years on the single datafile, see gnuplot stacked filledcurves can't show the corrects sum.

But I want to change and collect from multiple datafile with different date.
This is my previous graphs

graphs

I have multiple datafile with dates format. i.e:

200808  1
201104  2
201106  2
201107  4
201108  2
201109  4
201110  3
201111  2
201112  4
201201  7

and

200901  1
201101  3
201102  2
201103  2
201104  2
201105  2
201106  5
201107  12
201108  5
201109  24
201110  14
201111  18
201112  9

I have to show graphs by months. This is my graphs with single datafile.

set terminal png size 900, 300
set output "graphs.png"
set xdata time
set timefmt "%Y%m"
set xtics format "%b"
set grid
plot "data.dat" using 1:2 with lines lw 2 lt 3 title 'time'

Could you show me, how to change my script to support multiple datafile? thanks!

Community
  • 1
  • 1
indi60
  • 867
  • 4
  • 17
  • 34

1 Answers1

2

have all files read separately, as in:

file1 = 'data.dat'
file2 = 'data2.dat'

and then plot them one by one assuming that the number of files you have are manageable

set terminal png size 900, 300
set output "graphs.png"
set xdata time
set timefmt "%Y%m"
set xtics format "%b"
set grid
plot file2 using 1:2 with filledcurve x1 lt 3 title 'time1',\
file1 using 1:2 with filledcurve x1 lt 4 title 'time2'

If you have a large number of files and all end with .dat then you can do the following:

plot for [f in system("ls *.dat")] f using 1:2 with filledcurve x1 title(f)

However, it must be pointed out that the for loop may not give you the desired plot as curves are drawn over each other and if the "highest" curve is drawn the last, it will curve the entire plot.

About Stacking:

If your different data files only have some common timestamps, then you can prepare corresponding "trimmed" files which only have those common timestamps (across all your data files). The following bash script does that:

#!/bin/bash

tmp1="/tmp/tmp1$RANDOM"
tmp2="/tmp/tmp2$RANDOM"

cp data1.dat "$tmp1" # to start, assign one file to tmp1

for file in `ls *.dat`
do
    awk 'FNR==NR{a[$1]=$0;next}{if(b=a[$1]){print b}}' "$tmp1" "$file" | awk '{print $1}'  > "$tmp2"
    cp "$tmp2" "$tmp1"
done
cat "$tmp1" > common.dat # this will give you a file with all common timestamps

 # now trim all data files using the common.dat file generated in for loop above

for file in `ls *.dat`
do
    awk 'FNR==NR{a[$1]=$0;next}{if(b=a[$1]){print b}}' "$file" common.dat > trimmed_$file.dat
done
Zahaib Akhtar
  • 1,068
  • 2
  • 11
  • 26
  • Thanks @Zahaib Akhtar... Also I have to asking: how to make the graphs stacked? On my script, it just one line with single datafile. – indi60 Jun 06 '14 at 04:22
  • @indi60 basically you need stacked `filledcurves`. These links might be helpful: http://gnuplot.sourceforge.net/demo/fillcrvs.html http://newspaint.wordpress.com/2013/09/11/creating-a-filled-stack-graph-in-gnuplot/ – Zahaib Akhtar Jun 06 '14 at 04:29
  • @indi60, Please check my answer I've edited it with `filledcurve` – Zahaib Akhtar Jun 06 '14 at 04:46
  • I think the graphs is not stacked. I.e the second data should start from the end of the first data, and the third data should start from the last coordinate of the second data. do you get my points? – indi60 Jun 06 '14 at 06:54
  • @indi60 That is quite some work. Basically you must interpolate the data in every file such that you have the same dates in every file. Otherwise there is nothing to stack. – Christoph Jun 06 '14 at 06:57
  • @Christoph , I remember your's answer on my previous question about gnuplot. to do some like: sum [col=2:i] column(col). So, do you thing if we want to stacked date. we should set the same date in every data? – indi60 Jun 06 '14 at 07:05
  • Yes, I think so. In the data file you showed, you cannot stack e.g. the value `200901`, because you have no corresponding value in the first data file. I wouldn't know an other solution. – Christoph Jun 06 '14 at 07:09
  • I see, maybe I can thinker this problem with the set xrange.set xrange ["201106":"201112"] What do you think? – indi60 Jun 06 '14 at 07:19
  • If you have those values in all files, yes. Then you must use e.g. `paste` to merge all files on-the-fly so that you can access all values in order to stack the plots, see e.g. [Get ratio from 2 files in gnuplot](http://stackoverflow.com/a/20070138/2604213) – Christoph Jun 06 '14 at 07:34
  • yeah, I have not those values in all files... still thinking for this case – indi60 Jun 06 '14 at 07:58
  • @indi60 I'm also trying to understand what you mean by "stacking". The way you put it ("the second data should start from the end of the first data..") is not exactly what stacking would be. It should instead be layered on top of the other. That's why you would need common time stamps in your files. – Zahaib Akhtar Jun 06 '14 at 15:50
  • 1
    @indi60 Anyhow, I've added a bash script to my answer that will allow you to generate "trimmed" data files with timestamps such that all generated datafiles have common timestamps. I assume that all your files have atleast some common timestamps. If there is a file which has completely different timestamps then the resultant trimmed file for that will be empty. Which should be fine as it would've never gotten "stacked" in the first place. – Zahaib Akhtar Jun 06 '14 at 16:26
  • @indi60 However, if all your files have different timestamps then the added script will not help you much. In that case you will either need to interpolate or follow what Christoph has advised. – Zahaib Akhtar Jun 06 '14 at 17:20
  • I have merge my datafile only one file. but now I get problem to display on the date format. I will open new question about this. – indi60 Jun 10 '14 at 04:21