Showing posts with label background. Show all posts
Showing posts with label background. Show all posts

Monday, October 3, 2011

Round corner key box in gnuplot

In a previous post I talked about create a round corner rectangle in gnuplot. But none application was involved in that post. Today I use the method to create a round corner key box.
###############################################
#Variables:
##(a,b) is the low left vertex of the rectangle
##(c,d) is the up right vertex of the rectangle
##rx is the radius along x-axis
##ry is the radius along y-axis
##x is the independent variable of the curve
f_low(a,b,c,d,rx,ry,x)=a<x && x<a+rx ? \
     -ry*sqrt(1-((x-a-rx)/rx)**2)+b+ry : \
     a+rx<x && x<c-rx ? b :c-rx<x && x<c ?\
     -ry*sqrt(1-((x-c+rx)/rx)**2)+b+ry : 1/0
#The low curve of a round corner rectangle
f_up(a,b,c,d,rx,ry,x)=a<x && x<a+rx ?\
     ry*sqrt(1-((x-a-rx)/rx)**2)+d-ry : \
     a+rx<x && x<c-rx ? d :c-rx<x && x<c ?\
     ry*sqrt(1-((x-c+rx)/rx)**2)+d-ry : 1/0
#The up curve of a round corner rectangle
###############################################
reset
set term png font "Times,18"    #terminal and output file
set output "round_corner_rectangle_key_box.png"
set tics out nomirror
unset key    #key will be created manually
set sample 1000    #samples
#Setting the back ground color
set object 1 rect from graph 0,0 to graph 1,1 back
set object 1 rect fc rgb "#AAAAFF" fillstyle solid 1.0
#The text of the key (some people call it legend)
set label center "y=f(x)" at 5.75,0.7 front
#x and y label
set xlabel "x"
set ylabel "y=f(x)"
#Plot the curve,round corner rectangle and sample line of key
plot sin(5.*x)*exp(-x*x/20.) w l lw 2 lc rgb"green",\
     '+' u 1:(f_low(3.5,0.5,9,0.9,0.5,0.05,$1)):\
     (f_up(3.5,0.5,9,0.9,0.5,0.05,$1)) w filledcurve\
     lc rgb"pink" notitle,\
     x>7.5 && x<8.5 ?0.7:1/0 w l lw 2 lc rgb"green"
Nothing new in this script. The only important and difficult thing is deciding the position of the key text, key sample line and key box. At last we get a picture like this one.

Round corner key box in gnuplot




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

Thursday, September 1, 2011

Gnuplot advanced background color (1)

An alternative way to realize a linear gradient colored background is using the image plotting style. Let us first look at our final script and then explain it.
reset
set table "back.dat"	#creat a data file
set isosample 2,200	#containing linear
splot y			#gradient color 
unset table		#information

splot y	#Get the max value of z
max=GPVAL_Z_MAX

set term png
set output "backgradient2.png"
set multiplot
#plot the background
set lmargin at screen 0
set rmargin at screen 1
set bmargin at screen 0
set tmargin at screen 1
unset border
unset tics
set palette defined(0"white",max"#ccffcc")  #set gradient color
plot "back.dat" w ima
#plot the curve of sin(x)
set border 1+2+4+8
set tics
set lmargin
set rmargin
set bmargin
set tmargin
plot sin(x) w l lw 2 lc rgb"black" notitle
unset multiplot
!del back.dat
#in unix system the above command may should be change to "!rm back.dat"
In this script, firstly we produce a date file "back.dat" which contain linear gradient color information. Then in a multiplot environment we plot the background using command '''plot "back.dat" w ima''' and figure of sin(x) using '''plot sin(x) w l lw 2 lc rgb"black" notitle'''. Before plotting the background we set l,r,b,tmargin to be 0,1,0,1 respectively, thus the background is filled the whole picture, and border and tics are unset. After that all of border,margins and tics are set again. "! del back.dat" is used to delete temporary data file back.dat. backgradient2.png is show below:

Linear gradient colored background
 

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.
reset
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 \
behind
#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.