如何在gnuplot中重排并合并列?或用多列单输入绘图?
如何在gnuplot中重新排列多列并合并[name]_*格式列?
一、数据重排与合并方法
针对你提供的[name]_01/02格式列的数据,可通过以下gnuplot脚本直接生成符合要求的合并数据表:
方法1:使用gnuplot内部文件读取功能
创建并执行如下脚本:
# 设置输出文件路径 set output "merged_data.txt" # 定义打印格式,保持数据列对齐 set print format "%s %s %s" # 打印合并后的表头 print "agfc", "dfr", "ssft" infile = "你的输入数据文件名.txt" # 输出所有行的前3列([name]_01组) open(infile) getline line # 跳过原始表头 while (getline line) { split(line, cols, " ") print cols[1], cols[2], cols[3] } close(infile) # 输出所有行的后3列([name]_02组) open(infile) getline line # 跳过原始表头 while (getline line) { split(line, cols, " ") print cols[4], cols[5], cols[6] } close(infile) # 关闭输出 set output
方法2:调用外部工具快速处理
若习惯命令行工具,可通过system调用sed+awk完成:
set output "merged_data.txt" set print format "%s" print "agfc dfr ssft" # 输出[name]_01组数据 print system("sed '1d' 你的输入数据文件名.txt | awk '{print $1, $2, $3}'") # 输出[name]_02组数据 print system("sed '1d' 你的输入数据文件名.txt | awk '{print $4, $5, $6}'") set output
执行后,merged_data.txt将完全匹配你期望的数据格式。
二、plot命令能否将多列作为单个输入使用?
可以,gnuplot支持多种方式将多列数据合并为单个输入序列绘制,常见场景如下:
1. 基于合并后的数据文件绘制
若已生成merged_data.txt,直接指定列即可:
plot "merged_data.txt" using 0:1 with points title "agfc", \ "" using 0:2 with points title "dfr", \ "" using 0:3 with points title "ssft"
这里0代表行号作为x轴,1/2/3对应合并后的agfc/dfr/ssft列。
2. 直接使用原始多列数据绘制(无需提前合并)
不想生成中间文件时,可通过循环或手动指定列合并序列:
row_count = 15 # 原始数据的行数(不含表头) # 将agfc_01和agfc_02合并为一个y序列,x轴用连续行号 plot "你的输入数据文件名.txt" using 0:1 with points title "agfc", \ "" using ($0 + row_count):4 with points title "agfc"
也可通过循环批量处理多组列:
# 遍历2组数据,合并绘制agfc_01和agfc_02 plot for [i=1:2] "你的输入数据文件名.txt" using ($0 + (i-1)*row_count):(column(3*(i-1)+1)) with points title "agfc"
内容的提问来源于stack exchange,提问作者magfan
相关产品推荐
相关产品推荐

