jparanich
jparanich

Reputation: 8994

Plotting intersecting lines in GNUplot

I haven't been able to find any example of what I'm trying to do in GNUplot from raking docs and demos.

Essentially I want to plot the Blue, Green, and Red lines I manually drew on this output (for demonstration) at the 10/50/90% marks.

EDIT: For clarity, I'm looking to determine where the distribution lines hit the cumulative distribution at 0.1/0.5/0.9 to know which co-ordinates to draw the lines at. Thanks!

set terminal png size 1600,800 font "Consolas" 16
set output "test.png"

set title "PDF and CDF - 1000 Simulations"
set grid y2
set ylabel "Date Probability"

set y2range [0:1.00]
set y2tics 0.1
set y2label "Cumulative Distribution"

set xtics rotate by 90 offset 0,-5

set bmargin 6

plot "data.txt" using 1:3:xtic(2) notitle with boxes axes x1y1,'' using 1:4 notitle with linespoints axes x1y2 

enter image description here

Upvotes: 0

Views: 744

Answers (2)

theozh
theozh

Reputation: 25734

Depending on the number of points in your cumulative data curve you might need interpolation. The following example is chosen such that no original data point will be at your levels 10%, 50%, 90%. If your data is not steadily increasing, it will take the last value which matches your level(s). The procedure is as follows:

  1. plot your data to a dummy table.
  2. check when Level is between to successive y-values (y0,y1).
  3. remember the interpolated x-value in xp.
  4. draw arrows from the borders of the graph to the point (xp,Level) (or instead use the partly outside rectangle "trick" from @Ethan).

Code:

### linear interpolation of data
reset session
set colorsequence classic
set key left

# create some dummy data
set sample 10
set table $Data
    plot [-2:2] '+' u 1:(norm(x)) with table
unset table

Interpolate(yi) = x0 + (x1-x0)*(yi-y0)/(y1-y0)

Levels = "0.1 0.5 0.9"
do for [i=1:words(Levels)] {
    Level = word(Levels,i)
    x0 = x1 = y0 = y1 = NaN
    set table $Dummy
        plot $Data u (x0=x1,x1=$1,y0=y1,y1=$2, (y0<=Level && Level<=y1)? (xp=Interpolate(Level)):NaN ): (Level) w table
    unset table
    set arrow i*2   from xp, graph 0 to xp,Level nohead lc i
    set arrow i*2+1 from xp,Level to graph 1,Level nohead lc i
}

plot $Data u 1:2 w lp pt 7 lc 0 t "Original data"
### end code

Result:

enter image description here

Upvotes: 1

Ethan
Ethan

Reputation: 15093

It is not clear if you are asking how to find the x-coordinates at which your cumulative distribution line hits 0.1, 0.5, 0.9 (hard to do so I will leave that for now) or asking how to draw the lines once you know those x values. The latter part is easy. Think of the lines you want to draw as the unclipped portion of a rectangle that extends off the plot to the lower right:

set object 1 rectangle from x1, 0.1 to graph 2, -2 fillstyle empty border lc "blue"
set object 2 rectangle from x2, 0.1 to graph 2, -2 fillstyle empty border lc "green"
set object 3 rectangle from x3, 0.1 to graph 2, -2 fillstyle empty border lc "red"
plot ...

Upvotes: 1

Related Questions