Wednesday, August 31, 2011

Gnuplot advanced background color (0)

It is not so hard to plot a single color background graph using gnuplot, and we have done this in blog ---- simple background color. But how about a linear gradient colored background?

The fill color of a object can not be set to be gradient in gnuplot. So the method in simple background color can not be copied, at least can not be copied directly. But if we draw a set
of small rectangles with different color, it may have the same appearence as a large rectangle with linear gradient color. Now let us realize this idea.
n=200   #number of rectangles
dx=1.0/n    #legth of rectangle
set palette defined(0"white",n"#ccffcc")  #set gradient color
set cbrange [0:n]
unset colorbox
set for [i=1:n] object i rectangle from screen (i-1)*dx,0 to \
screen i*dx+dx/2,1 fs solid 1.0 noborder fillcolor palette cb i \
#draw a set of rectangle with diffrent color."+dx/2" makes the
#adjacent rectangles have a common part, so that the border is not
#so obvious
set term png
set output "backgradient1.png"
plot sin(x) w l lw 0 lc palette cb 0 notitle,sin(x) w l lw 2 lc \
rgb"blue" notitle
#The first plot is just used to enable the palette, and it will be
#covered by the second plot never appearing on the output picture.
set output
And the above commands produce the following graph:

Linear gradient colored background

Tuesday, August 30, 2011

Gnuplot simple background color

By default gnuplot will produce graphes with white background color. We can change it by drawing a colored rectangle at the lowest layer:
set object <index_number> rectangle \
         from screen 0,0 to screen 1,1 fillcolor rgb"<rgb-color>" behind 
In this command line, <index_number> is used to mark the rectangle object, if we want to change the background color later it will be usefull, "from screen 0,0 to screen 1,1" means the rectangle is as large as the whole picture, <rgb-color> is the background color and "behind" put the rectangle behind all other elements of the picture,i.e., the lowest layer.

Now let's have a look at an example script which produce two graphes with pink and light green background color respectively.
reset #reset gnuplot options
set term png font ",22" linewidth 3  #set terminal to png with fontsize=22,linewidth=3
set xlabel "x"  #set xlable and ylabel
set ylabel "y=sin(x)"
#set a pink(#ffcccc) background color
set object 1 rectangle from screen 0,0 to screen 1,1 fillcolor rgb"#ffcccc" behind
set output "pink.png"
plot sin(x) with lines linecolor rgb"green" notitle
#set a light green(#ccffcc) background color
set object 1 rectangle from screen 0,0 to screen 1,1 fillcolor rgb"#ccffcc" behind
set output "lightgreen.png"
plot sin(x) with lines linecolor rgb"red" notitle
And pink.png and lightgreen.png are shown below:

Pink background color

Light green background color

Creative Commons License
Except as otherwise noted, the content of this page is licensed under a Creative Commons Attribution-NonCommercial-NoDerivs 3.0 Unported License.