Gnuplot - intersection of two graphs

I am using gnuplot

to plot the data from two separate csv files (found at this link: https://drive.google.com/open?id=0B2Iv8dfU4fTUZGV6X1Bvb3c4TWs ) with a different number of lines that the following graph generates.

enter image description here

This data does not seem to have a common timestamp (first column) in both files csv

, and still gnuplot

seems to fit the plot as shown above.

Here is the gnuplot

script I am using to create my graph.

# ###### 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 a way that we can print (write to a file) the intersection values โ€‹โ€‹of these plots, ignoring the peaks above the green plot? I also tried doing the sql-join query, but it doesn't seem to print anything for the same reason I mentioned above.

PS: If the blue line is not touching the green line (i.e., if it is below the green line), I want to take the values โ€‹โ€‹of the nearest green line so that it is one-to-one (or very close) to the actual dataset.

+3


source to share


1 answer


It might be possible to somehow get Gnuplot to reinterpolate both datasets into a fine grid, store that ancillary data, and then compare them line by line. However, I find it much more practical to delegate this task to an external tool.

Of course, this is not the most efficient way to do it, however, the "lazy approach" might be to read the data points, interpret each dataset as a LineString (a set of line segments essentially equivalent to the assumption of linear interpolation between data points). and then calculate the intersection points. A Python script for 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 you can call 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: enter image description here

+5


source







All Articles