2012-06-18 135 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

相关问题