AskOverflow.Dev

AskOverflow.Dev Logo AskOverflow.Dev Logo

AskOverflow.Dev Navigation

  • 主页
  • 系统&网络
  • Ubuntu
  • Unix
  • DBA
  • Computer
  • Coding
  • LangChain

Mobile menu

Close
  • 主页
  • 系统&网络
    • 最新
    • 热门
    • 标签
  • Ubuntu
    • 最新
    • 热门
    • 标签
  • Unix
    • 最新
    • 标签
  • DBA
    • 最新
    • 标签
  • Computer
    • 最新
    • 标签
  • Coding
    • 最新
    • 标签
主页 / unix / 问题 / 522596
Accepted
Mr. 206
Mr. 206
Asked: 2019-06-04 04:27:50 +0800 CST2019-06-04 04:27:50 +0800 CST 2019-06-04 04:27:50 +0800 CST

Gnuplot:通过读取列标题从不同目录的 *txt 文件中绘图

  • 772

我的文件夹结构看起来像这样

$ tree
.
├── Original_folder
│   └── cat.txt
├── folderCD
│   └── cat.txt
├── folderGK
│   └── cat.txt
├── folderFE
    └── cat.txt

每个 cat.txt 文件在开始列标题之前有 5 行。示例 cat.txt 文件是这样的

Version LRv1.10.0
Build date 2017-12-06
MOL-calc
PRESSURE
!                       
      Time[s]     InletT[K]   InletP[Pa]   O2_GasOut     C_GasOut
       100         0.000885   1000000       0.0007       0.2111
and so on....

我想绘制第一列以及列标题带有关键字“_GasOut”的列。(此关键字的标题数量未知,对于每一列我想有一个单独的图表)。此外,对于folderCD、folderGK、folderFE……等所有图形,Original_folder 的图形结果应绘制在同一个图形中。

相应的图表应保存在相应的文件夹中,其标题与列标题相同。在每个图表中应该有两个图例一个是“original_folder”,另一个是“folderCD/folderGK/......”

我将 Original_folder 的所有输出绘图命令都放在一个 txt 文件中,并将所有其他文件夹的绘图命令放在另一个 txt 文件中。在那之后我没有找到继续前进的方法。我怎样才能为所有其他情况做到这一点?以及如何将列标题作为标题?

最近更新

for dir in folder* ; do
 echo "Preparing Post_processing files for ${dir}"
 mkdir "$dir"/Post_processing                        
 gawk -F  $'\t' '                                    
    /_GasOut/{                                      
       for(f=1;f<=NF;f++){                          
          hdr=$f                                    
         colhdr[f]=hdr                              
         if(index(hdr,"_GasOut"))wanted[f]=1
       }
    }
    ENDFILE{                                         

          print "reset\nset terminal pngcairo size 1024,768\nset encoding utf8\nset termoption dash\nset termopt enhanced"  
          print "set key top right"                                                                                         
          print "set xlabel '"'Time[s]'"';"    
       for(f in wanted){                           
          if(length(cmds)) cmds = cmds ",\n"
          hdr = colhdr[f]                        
          gsub(/^[[:space:]]+|[[:space:]]+$/,"",hdr)  
          printf("set ylabel '"'%s'"';\n",hdr)
          printf("set output '"'"$dir/Post_processing"/%s.png'"'\n",hdr)
          cmds = cmds "plot ""\"" FILENAME "\" using 1:" f " with lines" ","
          #print "plot " FILENAME using 1:" f " with lines" ",""
          cmds=cmds"'"'Original_folder/cat.txt'"' using 1:" f " with lines"        

        }      
       delete wanted  
    }
    END{              
       print cmds     
    }
    ' "$dir"/cat.txt>"$dir"/plot.gpl

   gnuplot "$dir"/plot.gpl
done

电流输出是这样的

reset
set terminal pngcairo size 1024,768
set encoding utf8
set termoption dash
set termopt enhanced
set xlabel 'Time[s]';
set ylabel 'H2_GasOut';
set output 'folderCD/Post_processing/H2_GasOut.png'
set ylabel 'O2_GasOut';
set output 'folderGK/Post_processing/O2_GasOut.png'
set ylabel 'H2O_GasOut';
set output 'folderFE/Post_processing/H2O_GasOut.png'
plot "folderCD/cat.txt" using 1:28 with lines,'Original_folder/cat.txt' using 1:28 with lines,
plot "folderGK/cat.txt" using 1:29 with lines,'Original_folder/cat.txt' using 1:29 with lines,
plot "folderGK/cat.txt" using 1:30 with lines,'Original_folder/cat.txt' using 1:30 with lines

期望的输出

reset
set terminal pngcairo size 1024,768
set encoding utf8
set termoption dash
set termopt enhanced
set xlabel 'Time[s]';
set ylabel 'H2_GasOut';
set output 'folderCD/Post_processing/H2_GasOut.png'
plot "folderCD/cat.txt" using 1:28 with lines,'Original_RedKinMec/cat.txt' using 1:28 with lines,
set ylabel 'O2_GasOut';
set output 'folderGK/Post_processing/O2_GasOut.png'
plot "folderGK/cat.txt" using 1:29 with lines,'Original_folder/cat.txt' using 1:29 with lines,
set ylabel 'H2O_GasOut';
set output 'folderFE/Post_processing/H2O_GasOut.png'
plot "folderGK/cat.txt" using 1:30 with lines,'Original_folder/cat.txt' using 1:30 with lines

有这样的输出也很好

set terminal pngcairo size 1024,768
   set encoding utf8
   set termopt dash
   set termopt enhanced
   set key top right
   set xlabel "Time[s]"
   set ylabel "O2_GasOut"
   set output "Post_processing/O2_GasOut.png"
   plot "folder1/cat.txt" using 1:22 with lines,\
   plot "folder2/cat.txt" using 1:22 with lines,\
   plot "folder3/cat.txt" using 1:22 with lines,\
   plot "folder4/cat.txt" using 1:22 with lines
   set ylabel "H2O_GasOut"
   set output "Post_processing/H2O_GasOut.png"
   plot "folder1/cat.txt" using 1:23 with lines,\
   plot "folder2/cat.txt" using 1:23 with lines,\
   plot "folder3/cat.txt" using 1:23 with lines,
   plot "folder4/cat.txt" using 1:23 with lines
   set ylabel "H2_GasOut"
   set output "Post_processing/H2_GasOut.png"
   plot "folder1/cat.txt" using 1:24 with lines,\
   plot "folder2/cat.txt" using 1:24 with lines,\
   plot "folder3/cat.txt" using 1:24 with lines,\
   plot "folder4/cat.txt" using 1:24 with lines




N.B: folder numbers are not fixed.
I added one of the cat.txt file for reference. https://1drv.ms/t/s!Aoomvi55MLAQh1wMmpnPGnliFmgg 
gnuplot
  • 1 1 个回答
  • 1850 Views

1 个回答

  • Voted
  1. Best Answer
    FelixJN
    2019-06-07T15:05:19+08:002019-06-07T15:05:19+08:00

    首先让我们拆分脚本,以便我们有一个bash脚本和一个awk脚本文件。这样,您将需要在脚本中进行较少的转义,并且可以使用-optionbash将变量转发到其中。awk-v

    for dir in folder* ; do
      echo "Preparing Post_processing files for ${dir}"
      mkdir "${dir}"/Post_processing
      gawk -f make_gpl.awk -v dirname="${dir}" "${dir}"/cat.txt > "${dir}"/plot.gpl
      gnuplot "${dir}"/plot.gpl
    done
    

    该bash脚本现在相当简单。

    脚本中的一些更正和简化awk- 我希望评论解释得很好:

    #inserted field separator definition into script
    BEGIN { FS="\t" }
    /_GasOut/{
       for(f=1;f<=NF;f++){
          # $a ~ "B" matches if string B is part of field $a
          # only these elements are taken to array colhdr
          if ($f ~ "_GasOut") colhdr[f]=$f
       }
    }
    ENDFILE{
          #split prints with newlines into separate splits for readability
          print "set terminal pngcairo size 1024,768
          print "set encoding utf8"
          print "set termopt dash"
          print "set termopt enhanced"  
          print "set key top right"                                                                                         
          print "set xlabel \"Time[s]\""
          #for loop only matches if element of array colhdr is set
          for(f in colhdr){
            #it looks like there are only preceding spaces
            gsub(/^ +/,"",colhdr[f])
            #printing strings only - no printf needed
            #escaping quotes if they need to be printed
            #removed semicolons and commas at end of plot command - newline will do
            print("set ylabel \""colhdr[f]"\"")
            print("set output \""dirname"/Post_processing/"colhdr[f]".png\"")
            print("plot \""FILENAME"\" using 1:"f" with lines")
          }      
    }    
    

    使用脚本和您创建的示例绘图文件cat.txt:

    set terminal pngcairo size 1024,768
    set encoding utf8
    set termopt dash
    set termopt enhanced
    set key top right
    set xlabel "Time[s]"
    set ylabel "O2_GasOut"
    set output "folder1/Post_processing/O2_GasOut.png"
    plot "folder1/cat.txt" using 1:22 with lines
    set ylabel "H2O_GasOut"
    set output "folder1/Post_processing/H2O_GasOut.png"
    plot "folder1/cat.txt" using 1:23 with lines
    set ylabel "H2_GasOut"
    set output "folder1/Post_processing/H2_GasOut.png"
    plot "folder1/cat.txt" using 1:24 with lines
    set ylabel "N2_GasOut"
    set output "folder1/Post_processing/N2_GasOut.png"
    plot "folder1/cat.txt" using 1:25 with lines
    set ylabel "NO_GasOut"
    set output "folder1/Post_processing/NO_GasOut.png"
    plot "folder1/cat.txt" using 1:26 with lines
    set ylabel "NO2_GasOut"
    set output "folder1/Post_processing/NO2_GasOut.png"
    plot "folder1/cat.txt" using 1:27 with lines
    set ylabel "N2O_GasOut"
    set output "folder1/Post_processing/N2O_GasOut.png"
    plot "folder1/cat.txt" using 1:28 with lines
    

    请注意,图中 y 标签的格式可能是错误的,但我不确定所需的格式。_将使以下字符下标为termopt enhanced. 要使更多字符变为下标,请使用括号,例如C_6H_{12}O_6糖的分子式。

    • 0

相关问题

  • 使用 GNUplot 设置轴标签和直方图标签

  • 如何从更改日志中显示 gnuplot 时间序列图

  • 使用 x 范围内具有相同值的文件的统计信息进行绘图

  • 重新排序 filledcurved 的绘图标题

  • 用文件中的点绘制二维图形

Sidebar

Stats

  • 问题 205573
  • 回答 270741
  • 最佳答案 135370
  • 用户 68524
  • 热门
  • 回答
  • Marko Smith

    模块 i915 可能缺少固件 /lib/firmware/i915/*

    • 3 个回答
  • Marko Smith

    无法获取 jessie backports 存储库

    • 4 个回答
  • Marko Smith

    如何将 GPG 私钥和公钥导出到文件

    • 4 个回答
  • Marko Smith

    我们如何运行存储在变量中的命令?

    • 5 个回答
  • Marko Smith

    如何配置 systemd-resolved 和 systemd-networkd 以使用本地 DNS 服务器来解析本地域和远程 DNS 服务器来解析远程域?

    • 3 个回答
  • Marko Smith

    dist-upgrade 后 Kali Linux 中的 apt-get update 错误 [重复]

    • 2 个回答
  • Marko Smith

    如何从 systemctl 服务日志中查看最新的 x 行

    • 5 个回答
  • Marko Smith

    Nano - 跳转到文件末尾

    • 8 个回答
  • Marko Smith

    grub 错误:你需要先加载内核

    • 4 个回答
  • Marko Smith

    如何下载软件包而不是使用 apt-get 命令安装它?

    • 7 个回答
  • Martin Hope
    user12345 无法获取 jessie backports 存储库 2019-03-27 04:39:28 +0800 CST
  • Martin Hope
    Carl 为什么大多数 systemd 示例都包含 WantedBy=multi-user.target? 2019-03-15 11:49:25 +0800 CST
  • Martin Hope
    rocky 如何将 GPG 私钥和公钥导出到文件 2018-11-16 05:36:15 +0800 CST
  • Martin Hope
    Evan Carroll systemctl 状态显示:“状态:降级” 2018-06-03 18:48:17 +0800 CST
  • Martin Hope
    Tim 我们如何运行存储在变量中的命令? 2018-05-21 04:46:29 +0800 CST
  • Martin Hope
    Ankur S 为什么 /dev/null 是一个文件?为什么它的功能不作为一个简单的程序来实现? 2018-04-17 07:28:04 +0800 CST
  • Martin Hope
    user3191334 如何从 systemctl 服务日志中查看最新的 x 行 2018-02-07 00:14:16 +0800 CST
  • Martin Hope
    Marko Pacak Nano - 跳转到文件末尾 2018-02-01 01:53:03 +0800 CST
  • Martin Hope
    Kidburla 为什么真假这么大? 2018-01-26 12:14:47 +0800 CST
  • Martin Hope
    Christos Baziotis 在一个巨大的(70GB)、一行、文本文件中替换字符串 2017-12-30 06:58:33 +0800 CST

热门标签

linux bash debian shell-script text-processing ubuntu centos shell awk ssh

Explore

  • 主页
  • 问题
    • 最新
    • 热门
  • 标签
  • 帮助

Footer

AskOverflow.Dev

关于我们

  • 关于我们
  • 联系我们

Legal Stuff

  • Privacy Policy

Language

  • Pt
  • Server
  • Unix

© 2023 AskOverflow.DEV All Rights Reserve