堆栈来自多个数据文件的行 [英] Stack lines from multiple data files

查看:140
本文介绍了堆栈来自多个数据文件的行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了堆叠图,其中x轴在单个数据文件上为年,请参阅 gnuplot堆叠已填充曲线无法显示更正总和

但是我想更改并从多个不同日期的数据文件中收集。

这是我以前的图表





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

  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

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

pre $ set terminal png size 900,300
set output graphs.png
set xdata time
set timefmt%Y%m
set xtics格式%b
set grid
plotdata.dat使用1:2与线lw 2 lt 3 title'time'

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

解决方案

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

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

,然后将它们一个接一个地假设为您可以管理的文件数量

  set terminal png size 900,300 
set outputgraphs.png
set xdata time
set timefmt%Y%m
set xtics格式%b
设置网格
绘制file2使用1:2与filledcurve x1 lt 3标题'time1',\
file1使用1:2与filledcurve x1 lt 4标题'time2'

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

 为[f in system(ls * .dat)] f使用1:2和filledcurve x1标题(f)
t,他的最高曲线是最后一条曲线,它会曲线绘制整个曲线图。


关于叠加:



如果您的不同数据文件只有一些常见时间戳,那么您可以准备相应的修剪文件,这些文件只有那些常见时间戳(跨越所有数据文件)。下面的bash脚本会这样做:

 #!/ bin / bash 

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

cp data1.dat$ tmp1#开始,将一个文件分配给tmp1

为`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
完成
猫$ tmp1> common.dat#这会给你一个带有所有常见时间戳的文件

#现在使用for循环中生成的common.dat文件修剪所有数据文件,文件位于

以上`ls * .dat`
do
awk'FNR == NR {a [$ 1] = $ 0; next} {if(b = a [$ 1]){print b}}'$ file common.dat> trimmed_ $ file.dat
完成


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

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!

解决方案

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

这篇关于堆栈来自多个数据文件的行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆