Gnuplot: conditional plotting ($2 == 15 ? $2 : '1/0') with lines

后端 未结 2 1023
萌比男神i
萌比男神i 2020-12-08 10:37

My data looks like this:

10:15:8:6.06000000:
10:15:2:19.03400000:
10:20:8:63.50600000:
10:20:2:24.71800000:
10:25:8:33.26200000:
10:30:8:508.23400000:
20:15:8:60.         


        
2条回答
  •  感动是毒
    2020-12-08 10:53

    ...just stumbled across this old question... Well, it's not "acceptable" that you need an external tool for such a basic task when you want to plot the filtered data connected with lines or with linespoints. There is a gnuplot-native solution. The "trick" of the workaround is to plot several data points on top of each other and only change the coordinates if a new point has been found. The code is as simple as this:

    ### conditional plot with connected lines or linespoints
    reset session
    
    # added two datapoints for testing purposes
    $Data <

    Result:

    Addition:

    just for completeness. Actually, set datafile missing "NaN" is solving the problem in gnuplot5.x, but since this question was from gnuplot4.6 times... and some people seem to still plot with version 4.x

    SO_Filter.dat

    # added two datapoints for testing purposes
    10:15:8:6.06000000:
    10:15:2:19.03400000:
    10:20:8:63.50600000:
    10:20:2:24.71800000:
    10:25:8:33.26200000:
    10:30:8:508.23400000:
    13:20:8:8.88888888:
    15:15:8:9.99999999:
    20:15:8:60.06300000:
    20:15:2:278.63100000:
    20:20:8:561.18000000:
    20:20:2:215.46600000:
    20:25:8:793.36000000:
    20:25:2:2347.52900000:
    20:30:8:5124.98700000:
    20:30:2:447.41000000:
    

    The code:

    ### conditional plot with connected lines or linespoints
    reset
    FILE = "SO_Filter.dat"
    set datafile separator ":"
    
    set multiplot layout 2,1  title "generated with gnuplot 4.6"
    
    # this works with gnuplot 4.x and 5.x
    x0 = y0 = NaN
    plot FILE u ($2==15 && $3==8 ? (y0=$4,x0=$1) : x0):(y0) w lp pt 7 ti "works with gnuplot >4.x and 5.x"
    
    # this works with gnuplot >5.x
    set datafile missing "NaN"
    plot FILE u ($2==15 && $3==8 ? $1 : NaN ):4 w lp pt 7 ti "works only with gnuplot >5.x"
    
    unset multiplot
    ### end of code
    

    Result in gnuplot 4.6:

提交回复
热议问题