Ron
Ron

Reputation: 177

In a Histogram chart(clustered), How to put vertical text in place of some of the bars, depending on condition?

In Gnuplot, I am using a Histogram chart(clustered), but not all data points have valid values. In those places, I want to replace the bar with a vertical text saying "Cannot Serve". How do I do it?

My current code:

set style data histogram
set style histogram cluster gap 2
set boxwidth 0.9
set xtic rotate by -45 scale 0 

set output "test.pdf"
plot 'data.txt' using 2:xtic(1) fs pattern 1 ti col, '' u 3 fs pattern 2 ti col

data file contains:

type "magnetic" "electric"
"high load" 12000 12721.033920
"med load" 15620.011886 15783.706215
"low load" 15636.000000 16254.000000

Upvotes: 1

Views: 145

Answers (1)

Miguel
Miguel

Reputation: 7627

This is a super hacky way to do this. I modified your file to add a "NaN":

"high load" NaN 12721.033920
"med load" 15620.011886 NaN
"low load" 15636.000000 16254.000000

Now I plot everything with boxes where the position of each box is calculated with respect to the order in which the records appear in the data file (column 0). It is "manually" defined here, but you should be able to write a function that gets the xrange and the box separation based on the number of records and the number of columns per record as obtained from stats for example. Also the boxwidth would depend on those values.

set xtic rotate by -45 scale 0
ymax = 20000
set yrange [0:ymax]

nrecords = 3
ncolumns = 2

set xrange [0:nrecords+1]

# Calculate boxwidth from available space per column
gap = 1./ncolumns/5.
width = 1./ncolumns/2.-gap/2.
set boxwidth width

plot "data.txt" u ($0+1.-width/2.-gap/2.):($2) w boxes t "data1", \
     "" u ($0+1.+width/2.+gap/2.):($3) w boxes t "data2", \
     "" u ($0+1.):(ymax/6.):(stringcolumn(2) eq "NaN" ? \
     "Cannot serve" : ""):xtic(1) w labels rotate by 90 offset \
     first -width/2.-gap/2.,0 not, \
     "" u ($0+1.):(ymax/6.):(stringcolumn(3) eq "NaN" ? "Cannot serve" \
     : ""):xtic(1) w labels rotate by 90 offset first width/2.+gap/2.,0 not

enter image description here

Upvotes: 1

Related Questions