Monday, 25 May 2009

3D bargraphs in gnuplot, once again

A couple of days ago, we discussed a quick and dirty (well, dirty, at least) way to generate 3D-looking bargraphs with gnuplot. There is another one (this time again with cylinders), which produces real-looking cylinders, like in this figure




The key to it is the 'phonging' procedure that we used yesterday to make that cute little sphere. So, here is the code. For ease of use, I defined the values that we want to plot in f1...f7 at the beginning of the file.

reset

A=-1
B=7
C=-4
D=3

# These are the actual values to be plotted
f1 = 1.0
f2 = 1.3
f3 = 0.4
f4 = 1.5
f5 = 1.9
f6 = 1.1
f7 = 0.9

# Radius of cylinders
r = 0.4
# Vertical position of 'phong' point
vp = 1.0

unset key
unset colorbox
set sample 100
set isosample 50, 50
set parametric
set urange [0:1]
set vrange [0:1]

set xrange [A:B]
set yrange [C:D]

# Basically, this is the definition of our cylinder

set table 'test.dat'
splot r*sin(2*pi*u), r*cos(2*pi*u), v,\
r*sin(2*pi*u)*v, r*cos(2*pi*u)*v, 1 w pm3d
unset table

set multiplot
set zrange [0:2]
unset xtics
unset ytics
set ztics out
set grid ztics
set ticslevel 0

# First, we draw the 'box' around the plotting volume
set palette model RGB functions 0.9, 0.9,0.95
splot A+(B-A)*u, C+(D-C)*v,0 w pm3d

# These are the vertical panes, with a gradient along
set palette model RGB functions 0.9, 0.9, 0.7+gray/5.0
splot A, C+(D-C)*u, 2*v w pm3d, A+(B-A)*u, D, 2*v w pm3d

set border 1+2+4+8+16+32+64+256+512
f(x,a,b) = 0.9*exp(-(x-a)*(x-a)/b/b)
set palette model RGB function gray, gray, 1
sp 'test.dat' u 1:2:($3*f1):(f($3*f1,vp,0.8)*f($1,0.3,0.2)*f($2,-0.3,0.2)) w pm
'' u ($1+1):2:($3*f2):(f($3*f2,vp,0.8)*f($1+1,1.3,0.2)*f($2,-0.3,0.2)) w pm3d,\
'' u ($1+2):2:($3*f3):(f($3*f3,vp,0.8)*f($1+2,2.3,0.2)*f($2,-0.3,0.2)) w pm3d,\
'' u ($1+3):2:($3*f4):(f($3*f4,vp,0.8)*f($1+3,3.3,0.2)*f($2,-0.3,0.2)) w pm3d,\
'' u ($1+4):2:($3*f5):(f($3*f5,vp,0.8)*f($1+4,4.3,0.2)*f($2,-0.3,0.2)) w pm3d,\
'' u ($1+5):2:($3*f6):(f($3*f6,vp,0.8)*f($1+5,5.3,0.2)*f($2,-0.3,0.2)) w pm3d,\
'' u ($1+6):2:($3*f7):(f($3*f7,vp,0.8)*f($1+6,6.3,0.2)*f($2,-0.3,0.2)) w pm3d

unset multiplot


If you look at it, this code is really nothing more than what we have already seen in previous days: apart from the general set-up commands, we plot the cylinder first to a file, (this time, we put on the cap, too), then draw the box in which we plot the bars. To make it more interesting, the vertical panes are given some gradient. You can check out the details of this in my previous post.

Finally, the cylinders are plotted one by one. Each time a new cylinder is processed, we shift it to the right, so that it doesn't overlap with the previous ones. To make the cylinders 3D-looking, we add the phong, as we discussed it yesterday. If you find that the white spot (actually, it is not completely white, but some very faint shade of blue, some steel-like colour) is too tight, you can ease up a bit on the Gaussian function. You can also shift the vertical position of the centre of the spot by tampering with the value of 'vp', somewhere at the beginning.


Again, it shouldn't be a problem to script the whole procedure. Once I have some time, I will do that and post it here. Till then!

No comments:

Post a Comment