跳轉到內容

GLPK/Gnuplot

來自華夏公益教科書

Gnuplot 是一個用於生成函式、資料和資料擬合的二維和三維圖表的程式。Gnuplot 在 GNU 通用公共許可證下發布。

二維直方圖

[編輯 | 編輯原始碼]
使用 gnuplot 從 GLPK 解生成的二維直方圖

Gnuplot 期望直方圖的資料以多列形式提供。

以下示例基於examples/transp.mod來自 GLPK 原始碼分發

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';
}

上面的 MathProg 語句(插入到transp.mod的資料語句之前,並另存為名為transp1.mod的檔案中)將使用以下命令建立:glpsol --math transp1.mod,在檔案transp1.dat:

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

中建立以下內容:transp1.dat可以使用 gnuplot 繪製

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)

的直方圖,然後將其儲存為 PNG 影像上面的命令可以手動輸入到互動式 gnuplot 會話中。呼叫gnuplot從命令列啟動這樣的會話。或者,可以將相同的命令儲存在文字檔案transp1.gp

gnuplot> load "transp1.gp"

中,然後作為指令碼從 gnuplot 中執行最後,使用任何點陣圖檢視器檢查生成的transp1.png

gthumb transp1.png &

,例如 gthumb

三維直方圖
[編輯 | 編輯原始碼]

使用 gnuplot 從 GLPK 解生成的 3D 直方圖

  • Gnuplot 不直接支援原生 3D 直方圖。可以使用以下規則將具有矩形網格的曲面傳遞給 gnuplot
  • 每個點提供一行文字,每個欄位之間用空格隔開
  • 同一柵格線上的連續點應位於連續的文字行上

在連續柵格線上的點之間放置一個空文字行。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;

要建立 3D 直方圖,需要提供直方圖每個柱子的 4 個角點。以下示例再次基於如前所述,可以使用 gnuplot 繪製transp2.dat

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
的 3D 直方圖,並將其儲存為 PNG 影像
華夏公益教科書