2017-05-30 173 views
3

我使用gnuplot繪製從兩個單獨的CSV文件中的數據的交叉點(在https://drive.google.com/open?id=0B2Iv8dfU4fTUZGV6X1Bvb3c4TWs此鏈接找到)。gnuplot的兩個曲線

enter image description here

這些數據似乎在兩個csv文件中沒有共同的時間戳(第一列)和尚未gnuplot似乎符合繪圖如上所示。

這是我用來生成我的情節的gnuplot腳本。

# ###### 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"; 

是否有任何方法可以通過忽略綠色圖上方的峯來打印(寫入文件)這些圖的交點值?我也試圖做一個sql連接查詢,但它似乎沒有打印出任何出於上述解釋相同的原因。如果藍線不接觸綠線(即,如果它低於綠線),我想採用最接近綠線的值,這樣它就成爲一對一的線,與實際數據集一致(或非常接近)。

+2

除非我很錯誤的是,Gnuplot是這項工作的錯誤工具。這是一個不用於數據操作或處理的程序。你想要一個通用的編程語言。 – Wrzlprmft

+0

你能否詳細說明你的意思是「交錯的情節」?你想保留「紫色數據」,只收獲「綠色數據」之上的內容? – ewcz

+0

我的意思是粉紅色線條和綠色線條相同(或接近相同),你可以從上面的圖中看到它。 –

回答

5

也許人們可能會強迫Gnuplot在一個細網格上重新插入兩個數據集,保存這些輔助數據,然後逐行比較它。但是,我認爲將這項任務委派給外部工具確實更爲實際。

這當然不是最有效的方法,但「懶惰的方法」可能是讀取數據點,將每個數據集解釋爲LineString(線段的集合,本質上等同於假設數據之間的線性插值點),然後計算交點。在Python,腳本要做到這一點可能是這樣的:

#!/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)) 
在gnuplot的

然後,一個可以直接調用它:

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 '' 

這給: enter image description here

+1

@DestaHaileselassieHagos你可以直接使用腳本並將其輸出重定向到一個文件,例如'python filter.py estimated.csv actual.csv> points.csv' – ewcz

+0

我做了,並且在這裏找到了帶有數據點的新圖: https://drive.google.com/open?id=0B2Iv8dfU4fTUZGV6X1Bvb3c4TWs。但是,過濾後的點數少於實際數據集的10%(這是基本事實)。如果藍線沒有觸及綠線,那麼讓我們將綠線的值與實際數據集進行一一對應(或非常接近)。讓我編輯我的問題,我會將您的答案標記爲已接受。 –

+0

你見過我最後一個問題ewcz嗎? –