如何在gnuplot中按行绘制 [英] how plot per rows in gnuplot

查看:123
本文介绍了如何在gnuplot中按行绘制的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个这种格式的文件

I have a file with this format

0   R1  R2  R3  R4
w1  I1  I2  I3  I4
w2  I1  I2  I3  I4
w3  I1  I2  I3  I4

在许多波长w中具有半径R和强度I的值.我想以2D方式绘制,在x轴上绘制第1行(半径),在y轴上绘制第3行(选择w2).

with values of radius R and intensity I in many wavelengths w. I want to plot in 2D, the row 1 (the radius) in the x-axis and row 3 in the y-axis (choosing w2).

如何绘制每行?不是按列

how plot per lines? not per columns

推荐答案

这也许不是最优雅的解决方案,但您可以先过滤两行(标头包含x值和对应于感兴趣波长的行) ,并排打印它们,最后使用Gnuplot绘制此辅助数据.

It's perhaps not the most elegant solution but you could first filter the two lines (the header containing the x-values and the line corresponding to the wavelength of interest), print them side by side, and finally plot this auxiliary data using Gnuplot.

为此,策略将是在您要从中执行Gnuplot的目录中创建一个名为filter.awk的脚本,其中包含以下内容:

To this end, the strategy would be to create a script called, e.g., filter.awk in the directory from which you want execute Gnuplot with following content:

NR==1{
    N = NF-1;
    for(i=1;i<=N;i++) x[i] = $(i+1);
    next;
}

$1==w{
    printf "#%f\n", $1;
    for(i=1;i<=N;i++) print x[i], $(i+1);
    printf "\n";
}

这会记住第一行的x值,并将它们存储在数组x中.波长通过变量w作为命令行参数(见下文)传递.如果数据的第一列等于w,则脚本将生成两列:第一列的x值,第二列的y值(从当前行获取).

This remembers the x-values from the first line and stores them in an array x. The wave length is passed as an command line argument (see below) via the variable w. If the first column in the data is equal to w, the script then generates two columns: x-values in the first one, y-values (taken from the current row) in the second one.

然后在Gnuplot中,您可以将其用作:

In Gnuplot you can then use this as:

plot \
    '<gawk -v w=100 -f filter.awk data.dat' w l

其中data.dat是您的输入数据,100的值表示应该过滤的波长.

where data.dat would be your input data and the value of 100 represents the wave length which is supposed to be filtered.

对此进行概括,例如可以这样做:

To generalize this, one could for example do:

cmd(w)=sprintf('<gawk -v w=%f -f filter.awk test.dat', w);

plot for [w in "100 200"] cmd(w+0) w l t sprintf('wave length of %.1f', w+0)

在这里,通过cmd函数生成绘制命令,该函数接受一个表示波长的参数.然后,plot命令遍历波长的列表",并使用自定义标题分别绘制它们.此处使用语句w+0w的字符串值显式转换为数字.

Here, the plotting command is generated via the cmd function which accepts one argument denoting the wave length. The plot command then loops over a "list" of wave lengths and plots them individually with custom title. The statement w+0 is used here to explicitly convert the string value of w to a number.

这篇关于如何在gnuplot中按行绘制的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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