如何在gnuplot中绘制多列文件

时间:2019-09-14 08:18:37

标签: colors gnuplot

我有一个多库伦文件,在某些行后有一个换行符。 看起来像

 # SET:   1
   0.00000 -62.49368   0.07000
   0.00639 -62.49367   0.07000
   0.01276 -62.49367   0.07000
   0.01914 -62.49366   0.07000
   0.02553 -62.49365   0.07000
   0.03190 -62.49364   0.07000
   0.03829 -62.49362   0.07000
   0.04467 -62.49361   0.07000
   0.05106 -62.49359   0.07000
   0.05743 -62.49356   0.07000

 # SET:   2
   0.00000 -62.49342   0.07000
   0.00639 -62.49342   0.07000
   0.01276 -62.49341   0.07000
   0.01914 -62.49340   0.07000
   0.02553 -62.49339   0.07000
   0.03190 -62.49338   0.07000
   0.03829 -62.49337   0.07000
   0.04467 -62.49335   0.07000

 # SET:   3
   0.00000 -62.47334   0.07000
   0.00639 -62.47225   0.07000
   0.01276 -62.47228   0.07000
   0.01914 -62.47231   0.07000
   0.02553 -62.47236   0.07000
   0.03190 -62.47242   0.07000
   0.03829 -62.47248   0.07000
   0.04467 -62.47256   0.07000
   0.05106 -62.47264   0.07000
   0.05743 -62.47273   0.07000
   0.06381 -62.47283   0.07000
   0.07020 -62.47296   0.07000
   0.07657 -62.47296   0.07000

以此类推。

我的情节是用

制作的
plot 'data' u 1:2 w l lc 2

y轴数据沿x轴倾斜,从负到正的比例范围大约为零。 我想用不同的颜色使顶线位于零以下和零以上(Y = 0处的水平线)。

要从数据文件中绘制在Y = 0(沿x轴)附近的数据集看起来像($ 2会将符号从负翻转为正)

 # SET:  38
   0.00000  -0.88752   0.07000
   0.00639  -0.88731   0.07000
   0.01276  -0.88751   0.07000
   0.01914  -0.88783   0.07000
   0.02553  -0.88827   0.07000
   0.03190  -0.88884   0.07000
   0.03829  -0.88954   0.07000
   0.04467  -0.89036   0.07000
   0.05106  -0.89132   0.07000
   0.05743  -0.89240   0.07000

 # SET:  39
   0.00000   2.02394   0.07000
   0.00639   2.02456   0.07000
   0.01276   2.02642   0.07000
   0.01914   2.02950   0.07000
   0.02553   2.03379   0.07000
   0.03190   2.03927   0.07000
   0.03829   2.04590   0.07000
   0.04467   2.05364   0.07000
   0.05106   2.06264   0.07000
   0.05743   2.07248   0.07000
   0.06381   2.08330   0.07000
   0.07020   2.09494   0.07000
   0.07657   2.10755   0.07000

因此,基本上我想使两条线都变彩色,从而将第2列的符号从负变为正。可以使用awk和grep命令对行进行grepped,可以看到,在此之后,SET $ 2从负变为正,然后可以在gnuplot中使用SET索引以使行变彩色。 不幸的是我不知道该怎么做。 希望我已经很好地解释了。如果需要进一步说明,请告诉我。

1 个答案:

答案 0 :(得分:0)

如果我对您的理解正确,那么当第2列中的值小于零时,您希望使该行具有一种颜色(例如,红色0xff0000),如果该行具有另一种颜色(例如,绿色0x00ff00)大于零。只需定义一个函数即可以0xRRGGBB的形式返回颜色代码。

也许是这样?

(如果要从文件中绘图,请跳过部分$Data <<EOD ... EOD,并在plot命令中将$Data替换为文件名,例如'MyFile.dat'

代码:

### color dependent on column value
reset session  
$Data <<EOD
 # SET:  38
   0.00000  -0.88752   0.07000
   0.00639  -0.88731   0.07000
   0.01276  -0.88751   0.07000
   0.01914  -0.88783   0.07000
   0.02553  -0.88827   0.07000
   0.03190  -0.88884   0.07000
   0.03829  -0.88954   0.07000
   0.04467  -0.89036   0.07000
   0.05106  -0.89132   0.07000
   0.05743  -0.89240   0.07000

 # SET:  39
   0.00000   2.02394   0.07000
   0.00639   2.02456   0.07000
   0.01276   2.02642   0.07000
   0.01914   2.02950   0.07000
   0.02553   2.03379   0.07000
   0.03190   2.03927   0.07000
   0.03829   2.04590   0.07000
   0.04467   2.05364   0.07000
   0.05106   2.06264   0.07000
   0.05743   2.07248   0.07000
   0.06381   2.08330   0.07000
   0.07020   2.09494   0.07000
   0.07657   2.10755   0.07000
EOD

myColor(n) = n<0 ? 0xff0000 : 0x00ff00
plot $Data u 1:2:(myColor($2)) w lp pt 7 lc rgb var notitle
### end of code

结果:

enter image description here

添加:

在评论中澄清后,以下可能是解决方案。

您将数据绘制到虚拟表中,并检查列2值小于零的最后一组。 column(-1)包含集合的编号(从零开始计数)。还要检查help pseudocolumns。 您可以稍微修改颜色功能,然后从数组中获取颜色。 经过gnuplot 5.2.6的测试。

代码:

### color dependent on column value
reset session

# create some random test data
set print $Data
Sets = 30
GapPos = int(rand(0)*Sets/3)+Sets/3
GapSize = 3
f(x) = (x-GapPos)+rand(0)*0.5 + (x>=GapPos ? GapSize : 0)
do for [i=1:Sets] {
    print "# SET:  ".i
    do for [j=1:10] {
        print sprintf("%.4f %.4f", (j+rand(0))/10., f(i))
    }
    print "\n"
}
set print

# find the SET-no. where column 2 values change from negative to positive
set table $Dummy
    plot $Data u (SetNo = $2<0 ? column(-1)+1 : SetNo) with table
unset table

# set the colors
array ArrColors[6] = [0xff0000, 0x00ff00, 0x0000ff, 0xff00ff, 0xffff00, 0x00ffff]
myColor(n) = n<SetNo-3 || n>=SetNo+3? 0xcccccc : ArrColors[n-SetNo+4]

plot $Data u 1:2:(myColor(column(-1))) w lp pt 7 lc rgb var notitle
### end of code

结果:

enter image description here

相关问题