Saturday, September 3, 2011

Shadow to a curve in Gnuplot

Adding shadow to a curve or some other objects can makes a 2-d plot looks like a 3-d one. The common way producing a shadow effect is drawing the object two times. Now let us come to an example of shadowing to a curve.
reset
set term png
set output "shadow1.png"
dy=0.75
dx=dy*tan(pi/6)
f(x)=0.1*(x-10)*x*(x+10)
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
Simple shadow effect
Firstly the shadow offset along y-axis and x-axis are given. The earlier plot is used to produce the shadow effcet and the later one is the data plotting. Note that the data plotting must be put after the shadow plotting, otherwise the "shadow" may cloud the data-curve. From the picture we find that the shadow effect have been realized, but it seems that the shadow is too sharp. We need to improve the script to get a better effect. Now let us do this. Our main idea is plotting the shadow sevral times with diffrent scale of gray colors. We realize this idea using a "for" iteration. The following is our improved script and the corresponding graph.
reset
set term png
set output "shadow2.png"
dy=0.75
dx=dy*tan(pi/6)
levels=15
f(x)=0.1*(x-10)*x*(x+10)
set palette gray
set cbrange [0:1]
unset colorbox
set xrange [-15:15]
set yrange [-200:200]
set multiplot
plot for [i=levels:1:-1] f(x-i*(1.0/levels)*dx)-i*(1.0/levels)*dy w l lw 7 \
      lc palette cb i*(0.5/levels)+0.35 notitle
plot f(x) w l lw 7 lc rgb"red" notitle
unset multiplot
Improved shadow effect

No comments:

Post a Comment

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.