1

我已经创建了堆叠图,它在单个数据文件上提供 x 轴是年份,请参阅gnuplot 堆叠填充曲线无法显示正确的 sum

但我想从具有不同日期的多个数据文件中更改和收集。
这是我以前的图表

图表

我有多个日期格式的数据文件。IE:

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

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

我必须按月显示图表。这是我带有单个数据文件的图表。

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'

你能告诉我,如何改变我的脚本来支持多个数据文件吗?谢谢!

4

1 回答 1

2

分别读取所有文件,如下所示:

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

然后假设您拥有的文件数量是可管理的,然后一张一张地绘制它们

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'

如果您有大量文件并且都以 .dat 结尾,那么您可以执行以下操作:

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

但是,必须指出,for 循环可能无法为您提供所需的图,因为曲线是相互绘制的,如果最后绘制“最高”曲线,它将使整个图弯曲。

关于堆叠:

如果您的不同数据文件只有一些共同的时间戳,那么您可以准备相应的“修剪”文件,这些文件只有那些共同的时间戳(在所有数据文件中)。以下 bash 脚本执行此操作:

#!/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
于 2014-06-06T03:51:52.273 回答