Search code examples
gnuplot

GNUPLOT draws step plot not within border restrictions


Drawing a step plot leads always to an out of border result.
How to solve the issue? Any ideas? THX!

The MWE is:

reset;set term png small size 500,500;set output 'test.png';
set title 'First step is always drawn out of chart borders ?!?';
unset y2tics;set y2range [0:40];set y2tics 10;set yrange [0:40];set ytics 10 mirror;
set style fill solid 1.00 border;
plot 'test.data' using 1:2  notitle with fillsteps lc rgb 'light-goldenrod', \
'' using 1:3 notitle with fillsteps lc rgb 'gray40', \
'' using 1:4 notitle with fillsteps lc rgb 'web-green', \
'' using 1:5 notitle with fillsteps lc rgb 'light-green';

The result is:

enter image description here

Used software is:

GNUPLOT Version 5.2 patchlevel 8


Solution

  • Ok, now I see your point. Looks like a little bug (or our limited understanding). I cannot tell right away why this is, but you can avoid it by adding a line in the beginning which contains the first x value and all y-values are 0. If you don't want to do this manually, there would be ways to do this automatically with gnuplot. But I hope there is a simpler solution.

    Code:

    ### plot with fillsteps
    reset session
    
    $Data <<EOD
    1    0   0   0   0
    1   50  35  30   5
    2   55  30  20   5
    17  51  44  30  12
    20   1   1   1   1
    EOD
    
    unset y2tics;set y2range [0:40]
    set y2tics 10
    set yrange [0:40]
    set ytics 10 mirror
    set style fill solid 1.00 border
    unset key
    
    plot $Data u 1:2 w fillsteps lc 'light-goldenrod', \
            '' u 1:3 w fillsteps lc 'gray40', \
            '' u 1:4 w fillsteps lc 'web-green', \
            '' u 1:5 w fillsteps lc 'light-green'
    ### end of code
    

    Result:

    enter image description here

    Addition: (automatically duplicate first line, to workaround the bug(!?))

    In order to workaround this (what I would call unexpected or a bug) you want to duplicate the first line automatically. There would be certainly different easy ways with external tools, however, which would not guarantee platform-independence. So, here is one of several possible gnuplot-only solutions.

    1. get your file into a datablock (here: $Data) (see gnuplot: load datafile 1:1 into datablock)
    2. print the first line of $Data into a new datablock (here: $Data2) Make sure that the first line is not a header or commented line, i.e. print the first dataline.
    3. append the full datablock $Data again to $Data2.

    Data: (Test.dat)

    1   50  35  30   5
    2   55  30  20   5
    17  51  44  30  12
    20   1   1   1   1
    

    Code: (Result same as above)

    # https://stackoverflow.com/a/67151340/7295599
    ### plot with filledcurves
    reset session
    
    FileToDatablock(f,d) = GPVAL_SYSNAME[1:7] eq "Windows" ? \
                           sprintf('< echo   %s ^<^<EOD  & type "%s"',d,f) : \
                           sprintf('< echo "\%s   <<EOD" & cat  "%s"',d,f)     # Linux/MacOS
    
    FILE = 'Test.dat'
    load FileToDatablock(FILE,'$Data')
    
    set print $Data2
        print $Data[1]   # only first line
        print $Data
    set print
    
    unset y2tics;set y2range [0:40]
    set y2tics 10
    set yrange [0:40]
    set ytics 10 mirror
    set style fill solid 1.00 border
    unset key
    
    plot $Data2 u 1:2 every ::0::0 w fillsteps lc 'light-goldenrod', \
            '' u 1:2 w fillsteps lc 'light-goldenrod', \
            '' u 1:3 w fillsteps lc 'gray40', \
            '' u 1:4 w fillsteps lc 'web-green', \
            '' u 1:5 w fillsteps lc 'light-green'
    ### end of code