2

I have data of protein molecular weights in column 6 of my file. The column in question looks like this:

MW [kDa]
16.8214045562515
101.41770820613989
24.332255496943485
43.946599899844436
210.58276787970942
57.987597263605494
27.384315650885558
119.02857910337919
8.962938979036466

I would like to plot a histogram and I am doing it using Gnuplot's smooth frequency function:

echo n=20 >$gnuplot #number of intervals
echo max=100 >> $gnuplot #max value
echo min=-0 >> $gnuplot #min value
echo width=\(max-min\)\/n >> $gnuplot #interval width
echo hist\(x,width\)=width*floor\(x\/width\)+width\/2.0 >> $gnuplot
echo plot \"$dataFile\" using \(hist\(\$6,width\)\)\:\(1.0\) smooth freq w boxes lc rgb\"blue\" notitle >> $gnuplot

How do I add a data label representing the count for each bin on top of each histogram bar? I cannot seem to find a way to do it.

nattzy
  • 87
  • 1
  • 6
  • Did you have a look at the plotting style `plot ... with labels` in the manual? Or for example this question: https://stackoverflow.com/questions/23177716/in-gnuplot-how-to-label-each-point-in-the-plot-with-its-coordinates – Eldrad Aug 05 '21 at 07:58
  • @ nattzy is your problem solved? Any kind of response would be polite and appreciated. – theozh Oct 20 '21 at 09:52
  • Yes, your solution helped me with my problem. Thank you! – nattzy Oct 21 '21 at 10:24

1 Answers1

2

I would plot the histogram data into a table first and then use this table for plotting the histogram itself and the labels. Check the following example. If you have a file, e.g. 'myData.dat', skip the random data generation lines, instead add the line FILE = 'myData.dat' and replace all $Data with FILE. As @Eldrad mentioned in the comments, use the plotting style with labels for the labels. Check help labels and help table.

Code:

### histogram with labeled bins
reset session

# create some random test data
set print $Data
    do for [i=1:2000] {
        print sprintf("%g",(invnorm(rand(0))+10)*20)
    }
set print

stats $Data u 1 nooutput
xmin = STATS_min
xmax = STATS_max

N = 20
myWidth = (xmax-xmin)/N
bin(col) = myWidth*floor(column(col)/myWidth)+myWidth/2.

set key noautotitle
set style fill solid 0.3
set boxwidth myWidth
set grid x,y
set offsets graph 0,0,0.05,0    # l,r,t,b

set table $Histo
    plot $Data u (bin(1)) smooth freq 
unset table

plot $Histo u 1:2 w boxes lc rgb "blue", \
         '' u 1:2:2 w labels offset 0,0.7
### end of code

Result:

enter image description here

theozh
  • 22,244
  • 5
  • 28
  • 72