Difference between revisions of "Graphics with gnuplot"

From Madagascar
Jump to navigation Jump to search
Line 134: Line 134:
 
End()
 
End()
 
</python>
 
</python>
 +
 +
To work with this SConstruct, the gnuplot scripts shown in the previous section (<tt>cosxpy_*.gp</tt>) must be modified by changing the last line from
 +
<pre>
 +
splot "cosxpy.z" matrix ...
 +
</pre>
 +
to
 +
<pre>
 +
splot "-" matrix ...
 +
</pre>

Revision as of 00:49, 26 June 2009

Gnuplot is included by default in the vast majority of Linux distributions and is also more flexible in certain ways than GLE.

Plotting using temporary ASCII files

Example SConstruct, that follows the one in the GLE example: <python> from rsfproj import *

o1=-2 o2=-2 n1=41 n2=41 d1=0.1 d2=0.1

Flow('cosxpy',None,

   
   math o1=%g o2=%g n1=%d n2=%d d1=%g d2=%g
         output="cos(x1*x1+x2*x2)*exp(-0.1*(x1*x1+x2*x2))"
    % (o1,o2,n1,n2,d1,d2))

Result('cosxpy','grey color=j')

  1. Prepare input for Gnuplot (ASCII file with data)

Flow('cosxpy.z','cosxpy',

   
   disfil number=n col=%d
    % n2)
  1. Draw surfaces

Result('cosxpy_iso','cosxpy_bsurf.gp cosxpy.z',

     
     gnuplot | epstopdf --filter
     ,suffix='.pdf')

End() </python> On some systems, the actual executable is not gnuplot, but gnuplot-minimal. Another dependency is the epstopdf utility, which is a part of LaTeX installation, usually. Gnuplot scripts can render the isometric view in slightly different ways. Three examples of Gnuplot scripts, and the images produced by each, follow:

cosxpy_bsurf.gp:

set terminal postscript eps enhanced color
set output

set title "Hat function (3D)"
set xtics ("-2" 0, "-1" 10, "0" 20, "1" 30, "2" 40) out
set ytics ("-2" 0, "-1" 10, "0" 20, "1" 30, "2" 40) out
set ztics out
set zrange [-1.5:1.5]
set ztics 0.5
set xlabel "X-axis"
set ylabel "Y-axis"
set zlabel "Z-axis" offset 3,-1
set ticslevel 0.5
set palette defined (-1 "blue", 0 "white", 1 "red")
set pm3d at b
splot "cosxpy.z" matrix with lines notitle

Gnuplot bsurf.png

cosxpy_cont.gp:

set terminal postscript eps enhanced color
set output

set title "Hat function (3D)"
set xtics ("-2" 0, "-1" 10, "0" 20, "1" 30, "2" 40) out
set ytics ("-2" 0, "-1" 10, "0" 20, "1" 30, "2" 40) out
set ztics out
set zrange [-1.5:1.5]
set ztics 0.5
set xlabel "X-axis"
set ylabel "Y-axis"
set zlabel "Z-axis" offset 3,-1
set ticslevel 0.5
set cntrparam levels auto 12
set contour base
set hidden3d offset 0
splot "cosxpy.z" matrix with lines notitle

Gnuplot cont.png

cosxpy_surf.gp:

set terminal postscript eps enhanced color
set output

set title "Hat function (3D)"
set xtics ("-2" 0, "-1" 10, "0" 20, "1" 30, "2" 40) out
set ytics ("-2" 0, "-1" 10, "0" 20, "1" 30, "2" 40) out
set ztics out
set zrange [-1.5:1.5]
set ztics 0.5
set xlabel "X-axis"
set ylabel "Y-axis"
set zlabel "Z-axis" offset 3,-1
set ticslevel 0.5
set palette defined (-1 "blue", 0 "white", 1 "red")
set pm3d at bs
splot "cosxpy.z" matrix with pm3d notitle

Gnuplot surf.png

Plotting without temporary ASCII files

Gnuplot can also read data from the standard input. Therefore there is no need to create a temporary ASCII file (cosxpy.z in the example). The SConstruct becomes even simpler, and it is more convenient, because one can use the same Gnuplot scripts to draw different data:

<python> from rsfproj import *

o1=-2 o2=-2 n1=41 n2=41 d1=0.1 d2=0.1

Flow('cosxpy',None,

   
   math o1=%g o2=%g n1=%d n2=%d d1=%g d2=%g
         output="cos(x1*x1+x2*x2)*exp(-0.1*(x1*x1+x2*x2))"
    % (o1,o2,n1,n2,d1,d2))

Result('cosxpy','grey color=j')

  1. Draw surface with Gnuplot

Result('cosxpy_iso','cosxpy cosxpy_bsurf.gp',

     
     disfil number=n col=%d | /bin/cat ${SOURCES[1]} - |
     gnuplot | epstopdf --filter
      % n2,suffix='.pdf')

End() </python>

To work with this SConstruct, the gnuplot scripts shown in the previous section (cosxpy_*.gp) must be modified by changing the last line from

splot "cosxpy.z" matrix ...

to

splot "-" matrix ...