从一个文件中绘制多个数据集 [英] Plot multiple datasets from one file

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

问题描述

注意:我可以控制数据文件的格式,但是它必须是单个文件.

Note: I can control the format of the data file, but it has to be a single file.

我正在尝试使用gnuplot在同一张图上绘制多个数据集.理想情况下,我想绘制以下内容:

I'm trying to plot multiple datasets on the same graph using gnuplot. I'd like ideally to plot something like this:

data_1 0 0
data_2 0 0
data_1 1 1
data_2 0 1
data_1 2 2
data_2 1 2

以此类推.在这种情况下,data_1data_2应该是两条单独的曲线.

And so on. In this case, data_1 and data_2 should be two separate curves.

我还想避免将gnuplot脚本(甚至可能是数据集的数量)放入gnuplot脚本中.基本上,我希望它通过特定的字段对数据点进行分组",然后将每个组作为一个单独的数据集绘制在同一张图上.

I'd also like to avoid putting in the gnuplot script the list, or even the number, of possible datasets. Basically, I'd like it to "group" data points by a specific field, and plot each group as a separate dataset on the same graph.

作为最后的替代方法,我可以使用grep将原始文件拆分为每个数据集一个文件,然后对它们进行绘图(我想这更容易吗?),但是我正在寻找一种方法来处理单个文件

As a last-resort alternative, I could split the original file into one file per dataset using grep, and plot those (I guess it's easier?), but I'm looking for a way to do it with a single file.

推荐答案

保存数据的gnuplot方法是用两条空行分隔数据集.然后,您可以使用index访问单个文件中的不同数据集:

The gnuplot-way to save your data would be to separate the data sets with two empty lines. Then you can use index to access the different data sets in the single file:

data_1 0 0
data_1 1 1
data_1 2 2


data_2 0 0
data_2 0 1
data_2 1 2

然后用

plot 'file.dat' using 2:3 index 0, '' using 2:3 index 1

要获取数据集的数量,请使用stats命令,该命令将数据集(数据块)的数量保存在一个变量中,该变量可用于迭代:

To get the number of data sets, use the stats command which saves the number of data sets (data blocks) in a variable which you can use for iterating:

stats 'file.dat' using 0 nooutput
plot for [i=0:(STATS_blocks - 1)] 'file.dat' using 2:3 index i

要扩展此范围,甚至可以按以下格式设置文件

To extend this, you could even format your file as follows

data_1
0 0
1 1
2 2


data_2
0 0
0 1
1 2

并使用第一行seach数据集作为绘图键:

and use the first line of seach data set as plot key:

set key autotitle columnheader
stats 'file.dat' using 0 nooutput
plot for [i=0:(STATS_blocks - 1)] 'file.dat' using 1:2 index i

这篇关于从一个文件中绘制多个数据集的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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