indi60
indi60

Reputation: 907

Stack lines from multiple data files

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!

Upvotes: 1

Views: 778

Answers (1)

Zahaib Akhtar
Zahaib Akhtar

Reputation: 1078

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

Upvotes: 2

Related Questions