Jump to content

GLPK/Gnuplot

From Wikibooks, open books for an open world

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

[edit | edit source]
2D histogram generated with gnuplot from a GLPK solution

Gnuplot expects data for histograms to be in multiple columns.

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

solve;
printf '""' > 'transp1.dat';
printf {j in J} ' "%s"', j >> 'transp1.dat';
printf '\n' >> 'transp1.dat';
for {i in I} {
  printf '"%s"', i >> 'transp1.dat';
  printf {j in J} ' %f', x[i,j] >> 'transp1.dat';
  printf '\n' >> 'transp1.dat';
}

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:

reset
set terminal png truecolor transparent font "Arial, 16" size 800x600
set output "transp1.png"
set title 'Result of transp.mod'
set style data histogram
set style histogram cluster gap 1
set style fill solid border −1
set boxwidth 0.9
set bmargin 5
set grid y
set xrange [−0.5:2.5]
set xtics out nomirror
plot 'transp1.dat' \
  using 2:xtic(1) title columnheader(2), \
  for [i=3:4] '' using i title columnheader(i)

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 transp1.png with any bitmap viewer, perhaps gthumb:

gthumb transp1.png &

3D histogram

[edit | edit source]
3D histogram generated with gnuplot from a GLPK solution

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 examples/transp.mod:

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 transp2.dat 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 −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