从gnuplot中的数据文件中选择一行 [英] Selecting a single row from a data file in gnuplot
问题描述
我有一个包含100列和数千行的数据文件,但是我希望能够选择一个单行,在该行中,将偶数列用作X轴的输入,奇数列用作图的Y轴输入。有什么办法可以在gnuplot中实现?
I have a data file with 100 columns and thousands of rows, but I want to be able to select one single row, and in that row, use the even columns as the inputs for the X-axis and the odd columns as inputs for the Y-axis for a plot. Is there any way I can do that in gnuplot?
推荐答案
所以在这里,我完成了从一个给定的数据文件,其中奇数列为x轴,偶数列为y轴。
So here the script I've done to plot a given line from a given data file with odd column as x-axis and even column as y-axis.
#!/usr/bin/gnuplot
set term pdf
set output "plot.pdf"
line_number=1
data_file="data.dat"
set xrange[0:10]
set yrange[0:10]
table_file="/tmp/gnuplot_tab.dat"
set table table_file
plot for[i=1:*:2] "<(sed -n '".line_number."p' ".data_file.")" u i:i+1
unset table
unset key
plot table_file
让我们解释一下此脚本:
Let's explain this script:
首先,我们使用 line_number
指定行号,并使用 data_file
指定数据文件名。由gnuplot 设置表table_file 的效果是 nofollow noreferrer>文档,即在文件̀table_file 中打印点的坐标,而不是使用
plot`命令对其进行打印。
First we specify the line number with line_number
and the data filename with data_file
. The effect of set table table_file
is, as specified by gnuplot documentation, the print of points' coordinates in the file ̀table_fileinstead of ploting them with
plot`command.
每个 i的[i = 1:*:2]
图的图
从 1
开始,在无法绘制更多列并在以下位置增加 2
时结束每次迭代。想法是采用 i
( ie 奇数)和 i + 1
( ie 偶数)作为坐标(或使用相反的 i + 1:i
取x轴为偶数,y轴为奇数)。
The plot for[i=1:*:2]
plots for each i
beginning from 1
, ending when no more column can be ploted and incrementing by 2
at each iteration. The idea is to take the columns i
(i.e. odd) and i+1
(i.e. even) as coordinates (or use the inverse i+1:i
to take even for x-axis and odd for y-axis).
部分<(sed -n' .line_number。 p' .data_file。)
受 Gnuplot从文件向上绘制数据的启发到某行,然后选择您指定为文件的行。由于完成了设置表
,因此此 plot
命令将每个坐标保存到一个新文件中。
The part "<(sed -n '".line_number."p' ".data_file.")"
is inspired from Gnuplot plotting data from a file up to some row and selects the line you specified as a file. Since set table
has been done, this plot
command saves each coordinate to a new file. This is a trick to transform the line in a two column file.
最后,脚本禁用bset table ,然后绘制保存的文件
Finally the script disable the ̀set tableto then plot the saved file
table_file`
我用以下数据文件对其进行了测试,将行号从 1
更改为 2
:
I tested it with the following data file changing the line number from 1
to 2
:
10 10 9 9 8 8 7 7 6 6 5 5 4 4 3 3 2 2 1 1 0 0
0 10 1 9 2 8 3 7 4 6 5 5 6 4 7 3 8 2 9 1 10 0
这篇关于从gnuplot中的数据文件中选择一行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!