gnuplot条形图中的不同颜色的条形图?

时间:2012-06-18 11:48:43

标签: gnuplot

我有一个非常简单的数据集:

Critical 2
High 18
Medium 5
Low 14

在此数据集中创建gnuplot中的条形图很简单,但所有条形图都是相同的颜色。我想拥有它,以便Critical是黑色,高是红色等等,但似乎没有任何在线教程可以做到这一点。

有人能指出我正确的方向吗?

2 个答案:

答案 0 :(得分: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)完成间距是数据集中的记录号(在本例中为行号)。

答案 1 :(得分: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

相关问题