使用来自多个文件的列数据进行Gnuplot绘图 [英] Gnuplot plotting using column data from multiple files

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

问题描述

我有两个文件"f.txt"和"g.txt".两个文件中的数据都排列在两列中,列分隔符只是几个空格.第一列代表自变量"x",第二列代表f(x)(或g(x)).绘制函数y = f(x)和y = g(x)很容易,但是有没有简单的方法可以绘制g(x)对f(x)呢?当然,使用bash,我可以创建带有第一列g(x)和第二列f(x)的中间"文件,然后正常绘制该文件.但是,是否有一种方法可以在不实际创建此文件的情况下进行此操作?

I have two files "f.txt" and "g.txt". The data in both files are arranged in two columns, the column separator being just a few whitespaces. The first column stand for the independent variable "x" and the second column for f(x) (or g(x)). It is easy to plot the function y= f(x) and y = g(x), but is there a simple way to plot f(x) against g(x)? Of course, using bash, I could create an "intermediary" file with first column g(x) and second column f(x) and then plotting normally this file. Is there however a way to do this without actually creating this file?

推荐答案

您的问题在一定程度上是矛盾的.看起来您有两个文件,每个文件都有两列,即 x,f(x) x,g(x).我假设行数和x的值相同.而且您简单地"想要绘制 f(x) g(x).但这与绘制 f(g(x))(在您的原始问题中)是相同的.在后一种情况下,您可能需要插值.

Your question was partly contradictory. It looked like you have two files with two columns each, i.e. x,f(x) and x,g(x). I assume that the number of rows and the values of x are identical. And you "simply" want to plot f(x) versus g(x). But this is not the same as plotting f(g(x)) (as in your original question). In the latter case you probably would need interpolation.

@Christoph已经指出了使用awk合并文件的简单解决方案.如果您没有或不想使用awk,这是一个有点麻烦的 gnuplot-only 解决方案.

@Christoph already pointed to a simple solution merging to files with awk. In case you don't have or don't want to use awk, here is a somewhat cumbersome gnuplot-only solution.

  1. 将文件1:1加载到数据块中(请参阅 gnuplot:将数据文件1:1加载到数据块中,但仍然不是完全回答)
  2. 删除行尾字符.这取决于操作系统.对于Windows,我必须将 \ r 作为行尾字符(出于某种原因而不是 \ r \ n ).目前,我无法测试Linux和MacOS.
  3. 将行打印/追加到新数据块
  1. load files 1:1 to datablock (see gnuplot: load datafile 1:1 into datablock, although, still not fully answered)
  2. remove the end of line character(s). This depends on the operating system. For Windows I have to put \r as end of line character (for some reason not \r\n). Currently, I cannot test Linux and MacOS.
  3. print/append lines to a new datablock

数据:

f.txt

A,B
1,3
2,6
3,5

g.txt

A,C
1,8
2,4
3,7

代码:

### merge two files (merging their lines)
# assumption: equal number of lines in each file
reset session

# load files 1:1 to datablocks
FILE1 = 'f.txt'
if (GPVAL_SYSNAME[:7] eq "Windows") { load '< echo   $Data1 ^<^<EOD  & type "'.FILE1.'"' }
else                                { load '< echo "\$Data1  << EOD" &  cat "'.FILE1.'"' } # Linux & MacOS
FILE2 = 'g.txt'
if (GPVAL_SYSNAME[:7] eq "Windows") { load '< echo   $Data2 ^<^<EOD  & type "'.FILE2.'"' }
else                                { load '< echo "\$Data2  << EOD" &  cat "'.FILE2.'"' } # Linux & MacOS

mySeparator = ","
myEOLc = sprintf("\r",0)     # End of Line character Linux: "\n", Windows: "\r\n", Mac: "\r"
# function for removing EOL character(s) from a string
RemoveEOLc(s) = s[strlen(s)-strlen(myEOLc)+1:strlen(s)] eq myEOLc ?  s[1:strlen(s)-strlen(myEOLc)] : s

set print $Data3
    do for [i=1:|$Data1|] {
        print sprintf("%s%s%s", RemoveEOLc($Data1[i]), mySeparator, RemoveEOLc($Data2[i]))
    }
set print

print $Data3
### end of code

结果:

A,B,A,C
1,3,1,8
2,6,2,4
3,5,3,7

然后根据需要绘制数据...

An then plot your data as you like...

添加:

如果文件具有空格作为列分隔符,则可以使用 word("string",n) $ Data [i] 中提取列"(请参见帮助字).然后,您需要更改以下行:

If your files have whitespace as column separator you can use word("string",n) to extract a "column" from $Data[i] (see help word). Then you need to change the lines:

mySeparator = " "

print sprintf("%s%s%s", word(RemoveEOLc($Data1[i]),2), mySeparator, word(RemoveEOLc($Data2[i]),2))

结果:

B C
3 8
6 4
5 7

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

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