Different coloured bars in gnuplot bar chart?

后端 未结 2 1935
生来不讨喜
生来不讨喜 2020-12-10 08:01

I have a very simple dataset:

Critical 2
High 18
Medium 5
Low 14

Creating a bar chart in gnuplot out of this dataset is easy, but all the b

2条回答
  •  北荒
    北荒 (楼主)
    2020-12-10 08:28

    set xrange [-.5:3.5]
    set yrange [0:]
    set style fill solid
    plot "

    This takes sed and triple spaces your file so that gnuplot sees each line as a different dataset (or "index"). You can plot each index separately using index or i for short as I have done. Also, the index number is available as column(-2) which is how we get the boxes properly spaced.

    Possibly a slightly more clean (gnuplot only) solution is using 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 doesn't depend on any particular ordering in your datafile (which is why I prefer it slightly to the other solution. We accomplish the spacing here with column(0) (or $0) which is the record number in the dataset (in this case, the line number).

提交回复
热议问题