Different color bars in gnuplot histogram?

I have a very simple data set:

Critical 2 High 18 Medium 5 Low 14 

Creating a histogram in gnuplot from this dataset is easy, but all the bars are the same color. I want to have it so that the critical is black, the red is high, etc., But it seems that there are hardly any online lessons for this.

Can someone point me in the right direction?

+6
source share
2 answers
 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" 

This takes up sed and triple the space of your file, so gnuplot sees each line as a different data set (or "index"). You can build each index separately using index <number> or i <number> for brevity, as I did. In addition, the index number is available as column(-2) , since we correctly distribute the fields.

Perhaps a slightly cleaner (gnuplot only) solution uses filters:

 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" 

This solution also does not depend on any particular order in your data file (therefore I prefer it to a slightly different solution. Here we execute the column(0) (or $0 ) interval, which is the number of the record in the data set (in this case, the line number )

+5
source

Here's how you can do this using the linecolor variable option.

If you know that rows are always in the same known order, you can use the row number (zero column, $0 ) as an index of the line type:

 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 

If the order may differ, you can use the gnuplot-style indexing function, which determines the warning level index from a string with spaces separated by spaces:

 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

+2
source

Source: https://habr.com/ru/post/918350/


All Articles