Different coloured bars in gnuplot bar chart?

后端 未结 2 1926
生来不讨喜
生来不讨喜 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:18

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

    If you know, that the lines are always in the same, known order, you can use the row number (zeroth column, $0) as linetype index:

    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 vary, you can use a gnuplot-style indexing function, which determines the index of the warning level from a string with space-separated words:

    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

提交回复
热议问题