GLPK/Gnuplot

Gnuplot is a program for generating two and three dimensional plots of functions, data, and data fits. Gnuplot is published under the GNU General Public License.

2D histogram


Gnuplot expects data for histograms to be in multiple columns.

The following example is based on examples/transp.mod from the GLPK source distribution:

The above MathProg statements (inserted before the data statements of transp.mod and saved as a file named transp1.mod) will create, using the command glpsol --math transp1.mod, the following content in the file transp1.dat:

"" "New-York" "Chicago" "Topeka" "Seattle" 50.000000 300.000000 0.000000 "San-Diego" 275.000000 0.000000 275.000000

Using gnuplot, a histogram of transp1.dat can be drawn and then saved as a PNG image:

The above commands can either be hand entered into an interactive gnuplot session. Invoke gnuplot from the command-line to start such a session. Alternatively, the same commands can be saved in a text file transp1.gp and then run as a script from within gnuplot:

gnuplot> load "transp1.gp"

Finally, check the resulting <tt>transp1.png</tt> with any bitmap viewer, perhaps gthumb:

gthumb transp1.png &

3D histogram


Gnuplot does not directly support native 3D histograms. Surfaces with rectangular grids can be passed to gnuplot using the following rules:


 * provide one text line per point, with each field separated by a space
 * consecutive points of one raster line should be on consecutive text lines
 * place a blank text line between points of consecutive raster lines.

To create a 3D histogram it is necessary to provide the 4 corner points of each pillar of the histogram. The following example is again based on <tt>examples/transp.mod</tt>:

solve;

printf '' > 'transp2.dat'; for { i in I } { for { j in J } { printf '%i "%s"', sum{k in I: k < i} 1, i >> 'transp2.dat'; printf ' %i "%s"', sum{l in J: l < j} 1, j >> 'transp2.dat'; printf ' %f', x[i,j] >> 'transp2.dat'; printf '\n' >> 'transp2.dat';

printf '%i "%s"', sum{k in I: k < i} 1, i >> 'transp2.dat'; printf ' %i "%s"', sum{l in J: l <= j} 1, '' >> 'transp2.dat'; printf ' %f', x[i,j] >> 'transp2.dat'; printf '\n' >> 'transp2.dat'; }   printf '\n' >> 'transp2.dat'; for { j in J } { printf '%i "%s"', sum{k in I: k <= i} 1, '' >> 'transp2.dat'; printf ' %i "%s"', sum{l in J: l < j} 1, j >> 'transp2.dat'; printf ' %f', x[i,j] >> 'transp2.dat'; printf '\n' >> 'transp2.dat';

printf '%i "%s"', sum{k in I: k <= i} 1, '' >> 'transp2.dat'; printf ' %i "%s"', sum{l in J: l <= j} 1, '' >> 'transp2.dat'; printf ' %f', x[i,j] >> 'transp2.dat'; printf '\n' >> 'transp2.dat'; }   printf '\n' >> 'transp2.dat'; } data;

set I := San-Diego Seattle;

set J := Chicago New-York Topeka;

As before, a 3D histogram of <tt>transp2.dat</tt> can be drawn using gnuplot and saved as a PNG image:

reset set terminal png font "Arial, 16" transparent size 800,800 set output "transp2.png" set title 'Result of transp.mod' set xtic offset first 0.5, first &minus;0.25, first 0 mirror set ytic offset first 0.25, first 0.5, first 0 mirror set nokey set pm3d set palette gray set grid x y z splot 'transp2.dat' using 1:3:5:xtic(2):ytic(4) with pm3d