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




4 comments:

  1. I'd like to reverse black and white on my plot, so this is a neat trick for the background. Do you know how one would then change the axis and text colour to white? I'm using epslatex terminal so I know that the labels can be changed simply by adding "\color{white} ..." before the text but I'm stumped as to how to change the rest.

    ReplyDelete
    Replies
    1. The color of lines and text are controled by parameter "linecolor"("lc" in short) and "textcolor"("tc" in short). For example, "plot sin(x) w l lc rgb"white" " will plot a white line and "set xtics tc rgb"white" " will change the text color of xtics to be white.

      Delete
  2. I have a multiplot graph and I'd like to use your method to set a transparent background. How can I get this?
    If I simple use your method, I get just one plot and background set up with the fixed color.
    I'm using python and svg output files.

    ReplyDelete
  3. To set a transparent background, you should use command "set style fill transparent". And using suitable coordinates in command "set object 1 rectangle from screen 0,0 to screen 1,1 fillcolor rgb"#ccffcc" behind", you can set the background color of defined area.

    ReplyDelete

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.