Suntory
Suntory

Reputation: 351

How to plot histogram with color function of a column value?

I Would like to plot a histogram but the color of my boxes should be function of a value inside a column. If the value in column 12 of my file is egal to 160 the color change in red.

my script :

set xlabel 'elapsed' font ",14"
set ylabel 'NCPUS' font ",14"
set xtics font "Verdana,12"
set style fill solid border -1
set xtic rotate by -45 scale 0


color(x)=(x==160?1:2)
#p "< sort 'histo' -k10n" using 12:xticlabels(($10)/3600) with histogram notitle ls color(12)


p "< sort 'histo' -k10n" using 12:xticlabels(($10)/3600) with boxes notitle ls color(12)

What I get :

enter image description here

my data :

     User        JobID    JobName  Partition      State  Timelimit               Start                 End    Elapsed ElapsedRaw   NNodes      NCPUS        NodeList 
--------- ------------ ---------- ---------- ---------- ---------- ------------------- ------------------- ---------- ---------- -------- ---------- --------------- 
  bouchet 6389               E423       epic  COMPLETED 365-00:00+ 2022-02-28T20:32:49 2022-03-04T11:24:48 3-14:51:59     312719        5        160        lio[1-5] 
          6389.batch        batch             COMPLETED            2022-02-28T20:32:49 2022-03-04T11:24:48 3-14:51:59     312719        1         32            lio1 
  bouchet 6393               Fenc       epic  COMPLETED  UNLIMITED 2022-03-04T12:08:07 2022-03-05T17:11:00 1-05:02:53     104573        5        160        lio[1-5] 
          6393.batch        batch             COMPLETED            2022-03-04T12:08:07 2022-03-05T17:11:00 1-05:02:53     104573        1         32            lio1 
  bouchet 6399               Fenc       epic  COMPLETED  UNLIMITED 2022-03-05T17:11:12 2022-03-08T10:07:10 2-16:55:58     233758        5        160        lio[1-5] 
          6399.batch        batch             COMPLETED            2022-03-05T17:11:12 2022-03-08T10:07:10 2-16:55:58     233758        1         32            lio1 

and when I use lc rgb variable I get :

boucher.plot", line 12: Not enough columns for variable color

Any ideas please ?

Upvotes: 1

Views: 158

Answers (1)

theozh
theozh

Reputation: 25749

I expected this to be a frequently asked question, however, it seems it would take more time to search for a suitable example than writing a few lines of code. The ingredients are lc rgb variable (check help variable) and ternary operator (check help ternary).

Script:

### variable color with boxes
reset session

# create some random test data
set table $Data
    myRandom(n) = int(rand(0)>0.5 ? 160 : rand(0)*160)
    plot '+' u 0:(myRandom(0)) w table
unset table

myColor(col) = column(col)>=160 ? 0xff0000 : 0x0ff00

set style fill solid 0.5 border lt -1
set key noautotitle
set xtics out

plot $Data u 1:2:(myColor(2)) w boxes lc rgb var
### end of script

Result:

enter image description here

Upvotes: 2

Related Questions