Friday, September 2, 2011

Gnuplot background image

Gnuplot can read png binary file and then plot it on the canvas. Using this utility we can add a background image to our plot. Let us see an example script.
reset
set term png
set output "world_population.png"
set multiplot
set xrange [0:799] 
set yrange [0:409]
#As the background picture's size is 800x410,
#we choose xrange and yrange of these values
unset tics
unset border
set lmargin at screen 0.175
set rmargin at screen 0.9
set bmargin at screen 0.15
set tmargin at screen 0.9
#Plot the background image
plot "map.png" binary filetype=png w rgbimage
#The x and y range of the population data file
set xrange [1740:2020]
set yrange [0:7000]
set border
set tics out nomirror scale 2
set mxtics 5
set key left
set xlabel "Year"
set ylabel "Population(in millions)"
plot "population.dat" u 1:2 w lp lw 2 ps 1 pt 7 title "world",\
     "population.dat" u 1:3 w lp lw 2 ps 1 pt 7 title "Africa",\
     "population.dat" u 1:4 w lp lw 2 ps 1 pt 7 title "Asia",\
     "population.dat" u 1:5 w lp lw 2 ps 1 pt 7 title "Europe",\
     "population.dat" u 1:6 w lp lw 2 ps 1 pt 7 title "Katub America",\
     "population.dat" u 1:7 w lp lw 2 ps 1 pt 7 title "Northern America",\
     "population.dat" u 1:8 w lp lw 2 ps 1 pt 7 title "Oceania"
unset multiplot
In this script, map.png is our background image with size 800x410 (That is why we choose xrange and yrange to be 0-799 and 0-409 respectively). population.dat is a file containing information of world population from 0 AD to 2000 AD. The first plot command is used to plot the background image, while the second plot command is used to plot our world population data file. To make these two plot coincide with each other, l,r,t,bmargin are set in the screen coordinate. The data file is as follows:
#Data from http://en.wikipedia.org/wiki/World_population
#Year  World  Africa  Asia  Europe  Latin America  Northern America  Oceania
1750  791  106  502  163  16  2  2  
1800  978  107  635  203  24  7  2  
1850  1262  111  809  276  38  26  2  
1900  1650  133  947  408  74  82  6  
1950  2519  221  1398  547  167  172  12.8  
1955  2756  247  1542  575  191  187  14.3  
1960  2982  277  1674  601  209  204  15.9  
1965  3335  314  1899  634  250  219  17.6  
1970  3692  357  2143  656  285  232  19.4  
1975  4068  408  2397  675  322  243  21.5  
1980  4435  470  2632  692  361  256  22.8  
1985  4831  542  2887  706  401  269  24.7  
1990  5263  622  3168  721  441  283  26.7  
1995  5674  707  3430  727  481  299  28.9  
2000  6070  796  3680  728  520  316  31.0  
2005  6454  888  3917  725  558  332  32.9  
2008  6707  973  4054  732  577  337  34.3
To use background image file of format other than png, we shold first convert it to a png file. This task can be done well using ImageMagick.

At last, this is the picture file world_population.png produced by the plotting script.

Gnuplot background image

7 comments:

  1. I get a failure when I try your idea:

    set term png
    "population.plt", line 2: You can't change the terminal in multiplot mode

    ReplyDelete
  2. Every time you using command "set multiplot", remember after the multiplot you should "unset multiplot".

    ReplyDelete
  3. Which version do you need to run this script?

    ReplyDelete
    Replies
    1. This script can be run under gnuplot 4.6 or 4.4.4 . And since this script is very simple, I think it can also be run under some lower version.

      Delete
  4. You would not guess how hard it is to find informastion on including watermarks in gnuplot. Most people seem to do this with external tools, and I even failed to locate it in the official documentation (used the wrong keywords for my searches obviously). Thanks for the explanation, that came very handy.

    ReplyDelete
  5. I would like to apply Arnold's cat map to an image.
    (sse pg 5 of http://www.maths.bristol.ac.uk/~maxcu/CATmap.pdf )

    is it feasible with gnuplot?

    ReplyDelete
    Replies
    1. It seems this is not a gnuplot feasible task. You konw gnuplot is a plotting a tool, not a data processing software.

      Delete

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.