2012-06-18 138 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" 

這需要sed和三重空間的文件,這樣的gnuplot看到每行一個不同的數據集(或「指數」)。您可以分別使用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 

如果順序可能會有所不同,您可以使用一個gnuplot的式索引函數,其確定從與空格分隔單詞的字符串的警告級別的索引:

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

相關問題