Search code examples
gnuplot

How to color the x<0 region of the plane in Gnuplot?


I am trying to color the x<0 region of a 2D plot, i.e, coloring the II and III region of the following picture

Picture

I have tried using

set style fill transparent solid 0.5 noborder
[-100:0] x<0 fs solid 1.0 lc rgb "grey" notitle

But it doesn't work, just gives a straight line instead of coloring the full halfplane. Any ideas?


Solution

  • You could place a rectangle behind the plot. Check the following:

    However, I haven't found out why the rectangle gets a border, although I specified set style fill solid 1.0 noborder.

    Edit: I still don't understand why gnuplot still draws a border. But I can get rid of the border when defining a fully transparent border, e.g. 0xff123456.

    set obj 1 rect from graph 0,0 to first 0, graph 1 fc rgb 0xccff0000 behind 
    set obj 1 fs solid 1.0 border rgb 0xff123456
    

    Code:

    ### half plot with colored background
    reset session
    set size ratio 1
    
    set border 0
    set xrange [-5:5]
    set yrange [-5:5]
    
    set xtics 1 axis
    set ytics 1 axis
    set xti
    set grid x,y
    
    set label 1 "I"   at graph 0.75, graph 0.75 font ",20"
    set label 2 "II"  at graph 0.25, graph 0.75 font ",20"
    set label 3 "III" at graph 0.25, graph 0.25 font ",20"
    set label 4 "IV"  at graph 0.75, graph 0.25 font ",20"
    
    set obj 1 rect from graph 0,0 to first 0, graph 1 fc rgb 0xccff0000 behind 
    set obj 1 fs solid 1.0 border rgb 0xff123456
    set xzeroaxis ls -1
    set yzeroaxis ls -1
    
    plot sin(x)
    ### end of code
    

    Result:

    enter image description here