I have been reading about coloring only certain histogram bars based on a property in a column in a data file. I need a boost in this area. Let's take a look at test code :
[1] data file:
1 4 3 this
2 7 21 this
3 11 43 that
4 5 6 this
5 2 51 that
[2] gnuplot code :
reset ;
set title "testing color of certain histogram bars \n \
based on property in data file (column 4)"
set bars back ;
set style data histogram ;
set style histogram cluster;
set style fill solid border -1 ;
set grid back ;
set grid xtics ytics mytics mxtics ;
set style line 2 lt 1 lc rgb "black" ;
plot newhistogram "", \
'test_08oct22a.dat' u 2:xticlabels(1) t 1, \
'' u 3:xticlabels(1) t 2
[ back to the question ]
I am stuck, having done unproductive work on this. I'd like to have gnuplot read the data file and, e.g., use "that" to color that bar green. "this" would be left alone. So if column 4 entry is "that", color the bar green. So bars (3, 43) and (5, 51) would be green, while all other bars use the initial color scheme. It helps to see the graphics here.
Here is what I tried:
I am focused on the part with e.g. xticlabels(1). I have found a couple ways to approach this - on the Stack Exchange or Stack Overflow sites - but do not understand them. one way was a conditional based on numerical value. Unfortunately, I seem to have lost the URL for this. One article that I found that is somewhat helpful is https://www.techrepublic.com/article/how-to-create-conditional-plotting-with-gnuplot/ :
u (column(0)):2:(0.5):($3=>1?1:2):xtic(1) w boxes lc variable
the inequality was working to some extent, but I do not understand how it is working in the rest of the code. I was confused by the use of xtic, lines, boxes, and such - the mytics and mxtics are being used as part of the larger script, so I kept them in to see if they are part of this problem.
Another approach that looked interesting was string matching, which I found a couple posts on :
Conditional plot according to a string value in gnuplot and make a condition in gnuplot when the valeur of column is a string
... so I am stuck - appreciate any pointers. The string matching would be preferable, if that helps.
some specs :
gnuplot 5.4 patchlevel 2
Ubuntu 22.04.1 LTS jammy
Actually, I would say there is no need to for style histogram, it's a simple bar chart (check help with boxes
).
You define a function myColor()
which uses the ternary operator (check help ternary
). If column 4 is equal to "that"
return 0x00ff00
(=green) and 0xff0000
(=red) otherwise. You need to use strcol()
and string comparison eq
.
There is no need for xtic()
if column1 is the linenumber.
Script:
### conditional color for boxes
reset session
$Data <<EOD
1 4 3 this
2 7 21 this
3 11 43 that
4 5 6 this
5 2 51 that
EOD
set boxwidth 0.3
set style fill solid 0.5
set xrange[0.5:5.5]
set key top left noautotitle
set grid y
myColor(col) = strcol(col) eq "that" ? 0x00ff00 : 0xff0000
plot $Data u ($1-0.2):2 w boxes lc rgb "red", \
'' u ($1+0.2):3:(myColor(4)) w boxes lc rgb var
### end of script
Result: