gnuplot-两个图的交集 [英] gnuplot - intersection of two plots

查看:94
本文介绍了gnuplot-两个图的交集的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 gnuplot 从两个单独的csv文件中绘制数据(在此链接中找到:



这些数据似乎没有通用的时间戳 csv 文件和 gnuplot 文件中的第一列(第一列)似乎都适合上述绘制。



这是我用来生成绘图的 gnuplot 脚本。

 ####### GNU绘图

设置样式数据线
设置终端脚本eps增强颜色时间 20

设置输出 output.eps

设置标题实际与估计比较

设置样式行99线型1 linecolor rgb# 999999 lw 2
#set border 1 back ls 11
set key right top
set key box linestyle 50
set key width -2
set xrange [0: 10]
设置键间距1.2
#设置nokey

设置网格xtics ytics神秘主义者
#set大小2
#set大小比率0.4

#显示时间戳记
设置xlabel时间[秒]
设置ylabel段

设置样式行1 lc rgb#ff0000 lt 1 pi 0 pt 4 lw 4 ps 0

使用($ 1):2行标题为 Estimated的 estimated.csv,使用($ 1):2行标题为 Actual的 actual.csv ;

有什么方法可以打印出(写入文件)通过忽略绿色图上方的峰来绘制这些图?我也尝试过执行sql-join查询,但是出于与我上面解释的相同原因,它似乎没有打印出任何内容。



PS:如果蓝线没有碰到绿线(即,如果它远低于绿线),我想获取最接近的绿色的值行,以便与实际数据集一一对应(或非常接近)。

解决方案

也许有人可能会以某种方式强迫Gnuplot在精细网格上重新插值两个数据集,保存此辅助数据,然后对其进行比较按行。但是,我认为将任务委派给外部工具确实更加实用。



这当然不是最有效的方法,但是懒惰的方法 可以是读取数据点,将每个数据集解释为LineString(线段的集合,基本上等同于假设数据点之间的线性插值),然后计算相交点。在Python中,执行此操作的脚本可能如下所示:

 #!/ usr / bin / env python 
import sys

从shapely.geometry导入numpy as np
import LineString
#--------------------- -------------------------------------------------- --------
def load_data(fname):
返回LineString(np.genfromtxt(fname,定界符=','))
#------- -------------------------------------------------- ----------------------
行=列表(map(load_data,sys.argv [1:]))

对于g in lines [0] .intersection(lines [1]):如果g.geom_type!='Point':
继续
print('%f,%f'% (gx,gy))

然后在Gnuplot中,可以直接调用它:

 设置终端pngcairo 
设置输出'fig.png'

设置数据文件分隔符逗号
设置yr [0:700]
设置xr [0:10]

设置xtics 0,2,10
设置ytics 0,100,700

设置网格

set xlabel时间[秒]
set ylabel段

地块\
'estimated.csv'wl lc rgb'深蓝色't'估计',\
'实际.csv'wl lc rgb'绿色't'实际',\
'< python filter.py估计.csv Actual.csv'wp lc rgb'红色'ps 0.5 pt 7 t''

可以得到:


I am using gnuplot to plot data from two separate csv files (found in this link: https://drive.google.com/open?id=0B2Iv8dfU4fTUZGV6X1Bvb3c4TWs) with a different number of rows which generates the following graph.

These data seem to have no common timestamp (the first column) in both csv files and yet gnuplot seems to fit the plotting as shown above.

Here is the gnuplot script that I use to generate my plot.

# ###### GNU Plot

set style data lines
set terminal postscript eps enhanced color "Times" 20

set output "output.eps"

set title "Actual vs. Estimated Comparison"

set style line 99 linetype 1 linecolor rgb "#999999" lw 2
#set border 1 back ls 11
set key right top
set key box linestyle 50
set key width -2
set xrange [0:10]
set key spacing 1.2
#set nokey

set grid xtics ytics mytics
#set size 2
#set size ratio 0.4

#show timestamp
set xlabel "Time [Seconds]"
set ylabel "Segments"

set style line 1 lc rgb "#ff0000" lt 1 pi 0 pt 4 lw 4 ps 0

plot  "estimated.csv" using ($1):2 with lines title "Estimated", "actual.csv" using ($1):2 with lines title "Actual";

Is there any way where we can print out (write to a file) the values of the intersection of these plots by ignoring the peaks above green plot? I also have tried to do an sql-join query but it doesn't seem to print out anything for the same reason I explained above.

PS: If the blue line doesn't touch the green line (i.e. if it is way below the green line), I want to take the values of the closest green line so that it will be a one-to-one correspondence (or very close) with the actual dataset.

解决方案

Perhaps one could somehow force Gnuplot to reinterpolate both data sets on a fine grid, save this auxiliary data and then compare it row by row. However, I think that it's indeed much more practical to delegate this task to an external tool.

It's certainly not the most efficient way to do it, nevertheless a "lazy approach" could be to read the data points, interpret each dataset as a LineString (collection of line segments, essentially equivalent to assuming a linear interpolation between data points) and then calculate the intersection points. In Python, the script to do this might look like this:

#!/usr/bin/env python
import sys

import numpy as np
from shapely.geometry import LineString
#-------------------------------------------------------------------------------
def load_data(fname):
    return LineString(np.genfromtxt(fname, delimiter = ','))
#-------------------------------------------------------------------------------
lines = list(map(load_data, sys.argv[1:]))

for g in lines[0].intersection(lines[1]):
    if g.geom_type != 'Point':
        continue
    print('%f,%f' % (g.x, g.y))

Then in Gnuplot, one can invoke it directly:

set terminal pngcairo
set output 'fig.png'

set datafile separator comma
set yr [0:700]
set xr [0:10]

set xtics 0,2,10
set ytics 0,100,700

set grid

set xlabel "Time [seconds]"
set ylabel "Segments"

plot \
    'estimated.csv' w l lc rgb 'dark-blue' t 'Estimated', \
    'actual.csv' w l lc rgb 'green' t 'Actual', \
    '<python filter.py estimated.csv actual.csv' w p lc rgb 'red' ps 0.5 pt 7 t ''

which gives:

这篇关于gnuplot-两个图的交集的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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