Graph 从多个数据文件堆栈行

Graph 从多个数据文件堆栈行,graph,gnuplot,stacked,Graph,Gnuplot,Stacked,我已经创建了堆叠图,在单个数据文件中提供x轴是年,请参阅 但我想更改和收集多个日期不同的数据文件。 这是我以前的图表 我有多个日期格式的数据文件。i、 e: 200808 1 201104 2 201106 2 201107 4 201108 2 201109 4 201110 3 201111 2 201112 4 201201 7 及 我必须按月显示图表。 这是我的带有单个数据文件的图形 set terminal png size 900, 300 set outpu

我已经创建了堆叠图,在单个数据文件中提供x轴是年,请参阅

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

我有多个日期格式的数据文件。i、 e:

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

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

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'

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

单独读取所有文件,如:

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

谢谢@Zahaib Akhtar。。。我还要问:如何使图形堆叠?在我的脚本中,它只包含一行数据文件。@indi60基本上你需要堆叠的
填充曲线
。这些链接可能会有帮助:@indi60,请检查我的答案。我用
filledcrove
编辑了它。我认为这些图没有堆叠。即,第二个数据应从第一个数据的末尾开始,第三个数据应从第二个数据的最后一个坐标开始。你明白我的意思吗?@indi60无论如何,我已经在我的答案中添加了一个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