Showing posts with label legend. Show all posts
Showing posts with label legend. 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




Sunday, September 4, 2011

Shadow to the key in Gnuplot

Yesterday we talk about shadow to a curve in gnuplot . But the key is not plotted. Now we come to add a shadowed key to our graph. The method is the same as before, plotting the objects two or more times. The following is an example gnuplot script.
reset
set term png
set output "shadowkey.png"
dy=0.75
angle=pi/6.0
dx=dy*tan(angle)
f(x)=0.1*(x-10)*x*(x+10)
set object 1 rectangle from graph 0.91,0.89 \
    to graph 0.66,0.74 fillstyle solid 1.0 noborder \
    fc rgb"#cccccc"	#key-box shadow
set object 2 rectangle from graph 0.9,0.9 \
    to graph 0.65,0.75 fc rgb"#ffffff"    #key-box
set arrow 1 lw 7 lc rgb"#cccccc" from graph 0.78,0.82 \
    to graph 0.86,0.82 nohead #samle-line shadow
set arrow 2 lw 7 lc rgb"red" from graph 0.77,0.83 \
    to graph 0.85,0.83 nohead     #sample-line
set label 1 "f(x)" at graph 0.7,0.83	#key label
set xrange [-15:15]
plot f(x-dx)-dy w l lw 7 lc rgb"#cccccc" notitle,\
     f(x) w l lw 7 lc rgb"red" notitle
Because gnuplot can not give us enough control on the key by using command "set key ...", in this script we draw the key manually using rectangles,arrows and label. The final appearance of shadow1.png is shown below.

Shadow to the key in gnuplot

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.