2012-06-18 51 views
6

私は非常に単純なデータセットがあります。gnuplot棒グラフの異なる色の棒グラフですか?

Critical 2 
High 18 
Medium 5 
Low 14 

は、このデータセットのうち、gnuplotので棒グラフを作成することは簡単ですが、すべてのバーは同じ色です。クリティカルが黒、ハイが赤などになるようにしたいが、これを行うためのオンラインチュートリアルはほとんどないようだ。

誰でも正しい方向に向けることができますか?

答えて

5
set xrange [-.5:3.5] 
set yrange [0:] 
set style fill solid 
plot "<sed 'G;G' test.dat" i 0 u (column(-2)):2:xtic(1) w boxes ti "Critical" lc rgb "black",\ 
    "<sed 'G;G' test.dat" i 1 u (column(-2)):2:xtic(1) w boxes ti "High" lc rgb "red" ,\ 
    "<sed 'G;G' test.dat" i 2 u (column(-2)):2:xtic(1) w boxes ti "Medium" lc rgb "green",\ 
    "<sed 'G;G' test.dat" i 3 u (column(-2)):2:xtic(1) w boxes ti "Low" lc rgb "blue" 

gnuplotは異なるデータセット(または「インデックス」)として各行を見ているように、これはsed、トリプルスペース、あなたのファイルを取ります。私が行ったように、index <number>またはi <number>を使用して、各インデックスを別々にプロットすることができます。また、インデックス番号はcolumn(-2)として入手できます。これは、適切な間隔でボックスを取得する方法です。おそらく

もう少しクリーン(gnuplotは唯一の)解決策は、フィルタを使用している:

set xrange [-.5:3.5] 
set yrange [0:] 
set style fill solid 
CRITROW(x,y)=(x eq "Critical") ? y:1/0 
HIGHROW(x,y)=(x eq "High") ? y:1/0 
MIDROW(x,y) =(x eq "Medium") ? y:1/0 
LOWROW(x,y) =(x eq "Low") ? y:1/0 
plot 'test.dat' u ($0):(CRITROW(stringcolumn(1),$2)):xtic(1) w boxes lc rgb "black" ti "Critical" ,\ 
    '' u ($0):(HIGHROW(stringcolumn(1),$2)):xtic(1) w boxes lc rgb "red" ti "High" ,\ 
    '' u ($0):(MIDROW(stringcolumn(1),$2)):xtic(1) w boxes lc rgb "green" ti "Medium" ,\ 
    '' u ($0):(LOWROW(stringcolumn(1),$2)):xtic(1) w boxes lc rgb "blue" ti "Low" 
このソリューションはまた、私は他に少しそれを好む理由である(あなたのデータ・ファイル内の任意の特定の順序に依存しない

(この場合、行番号)データセット内のレコードの数であるソリューションを提供します。我々はcolumn(0)(または$0)とここの間隔を達成。

2

ここでは、あなたがlinecolor variableオプションを使用して、これを行うことができる方法である。

線が同じ、既知の順序で常に、あなたは線種指標として行番号(0列、$0)を使用することができ、あなたが知っている場合:順序は変えることができる場合

set style fill solid noborder 
set linetype 1 lc rgb 'black' 
set linetype 2 lc rgb 'red' 
set linetype 3 lc rgb 'yellow' 
set linetype 4 lc rgb 'green' 

set yrange [0:*] 
unset key 
plot 'alerts.txt' using 0:2:($0+1):xtic(1) with boxes linecolor variable 

、あなたが使用することができ

alerts = 'Critical High Medium Low' 
index(s) = words(substr(alerts, 0, strstrt(alerts, s)-1)) + 1 

set style fill solid noborder 
set linetype 1 lc rgb 'black' 
set linetype 2 lc rgb 'red' 
set linetype 3 lc rgb 'yellow' 
set linetype 4 lc rgb 'green' 

set yrange [0:*] 
unset key 
plot 'alerts.txt' using 0:2:(index(strcol(1))):xtic(1) with boxes linecolor variable 

enter image description here

:スペースで区切られた単語の文字列から警告レベルの指標を決定gnuplotのスタイルのインデックス機能、