如何使用Gnuplot绘制几个图,这些图表示一个数据集中来自数据集的多个子集行? [英] How can I plot several diagrams that represent several subset rows from the dataset in one plot using Gnuplot?

查看:35
本文介绍了如何使用Gnuplot绘制几个图,这些图表示一个数据集中来自数据集的多个子集行?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个数据集,其名称为('output.txt'),格式如下:

  1 ......2 ......4 .....6 ......7 .....10 ......1 ......2 ......5 .....6 ......7 .....1 ......3 ......4 .....6 ......7 .....10 ............2 ......4 .....6 ......7 .....10 ...... 

我想在其自己的彩色图中绘制行的每个子集(直到我达到x + 1的值小于x的值),然后在一个图中绘制所有图.如何使用Gnuplot做到这一点?

解决方案

从您不完整的示例数据来看,每个块的x值看起来并不相同,每个块的x值数量甚至也不相同.如果正常,例如每个块总是6个值,您可以简单地将 plot用于 every ,检查 help plot for help .但这在您的情况下是不正常的.

为了绘制您的每个块",用不同的颜色,您必须告诉gnuplot块在哪里结束.通常,这是用一个或几个空行完成的.不幸的是,您的块之间没有空线,因此您必须自己插入它们.通过外部工具或gnuplot本身(后者将花费一些额外的代码行).这里有必要将数据放在一个数据块中.如何将文件放入其中,请参阅(

I have a dataset has a name ('output.txt') and formatted as follows:

    1   ......
    2   ......
    4   .....
    6   ......
    7   .....
    10   ......
    1   ......
    2   ......
    5   .....
    6   ......
    7   .....
    1   ......
    3   ......
    4   .....
    6   ......
    7   .....
    10   ......
.
.
.
.
.
.
    2   ......
    4   .....
    6   ......
    7   .....
    10   ......

I want to plot each subset of rows (until I reach the value of x+1 is less than the value of x) in its own colored diagram, and all the diagrams in one plot. How can I do this using Gnuplot?

解决方案

From your incomplete example data it looks like the x-values are not the same for each block and also even not the same amount of x-values per block. If it was regular, e.g. always 6 values for each block you could simply use plot for and every, check help plot for and help every. But it is not regular in your case.

In order to plot each of your "blocks" with different color, you have to tell gnuplot where the blocks end. Usually this is done with one or several empty lines. Unfortunately, you don't have empty lines between your blocks, so you have to insert them yourself. Either by an external tool or by gnuplot itself (the latter will take a few extra lines of code). Here it is necessary to have the data in a datablock. How to get a file into there, see (gnuplot: load datafile 1:1 into datablock)

A second approach (if you want to plot with linespoints) would be to plot twice once with points and a second time with lines where the color is changed every time when the current x-value is smaller than the previous value. In order to make a split between the blocks, the color is set to invisible (myLineC=-2). If you only want lines you can simply skip the plot with points. Depending on your exact needs (colors, key, etc. ) it maybe need to be further fine tuned.

Code:

### split data based on x-values
reset session

$Data <<EOD
    1   0.11
    2   0.12
    4   0.13
    6   0.14
    7   0.15
    10  0.16
    1   0.21
    2   0.22
    5   0.23
    6   0.24
    7   0.25
    1   0.31
    3   0.32
    4   0.33
    6   0.34
    7   0.35
    10  0.36
    2   0.41
    4   0.42
    6   0.43
    7   0.44
    10  0.45
EOD

# first approach by inserting empty lines
set title "Approach 1"
set print $DataSplit
    x0=NaN
    do for [i=1:|$Data|] {
        x1 = real(word($Data[i],1))
        if (x1<x0) { print "\n" }
        x0 = x1
        print $Data[i]
    }
set print
plot $DataSplit u 1:2:(column(-1)+1) w lp pt 7 lc var notitle, \

pause -1 "Press Enter to continue"

# second approach by setting the color invisible to split
set title "Approach 2"
myColor = 1
x0=NaN
plot $Data u ((x0>$1 ? (myColor=myColor+1,myLineC=-2) : myLineC=myColor), x0=$1):2:(myLineC) w l lc var notitle, \
    tmp=(x0=NaN, myColor=1) $Data u (($1<x0 ? myColor=myColor+1 : 0), x0=$1):2:(myColor) w p pt 7 lc var notitle
### end of code

Result: (twice the same result)

这篇关于如何使用Gnuplot绘制几个图,这些图表示一个数据集中来自数据集的多个子集行?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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