Search code examples
gnuplothistogram

Error "xrange is invalid" when trying to plot an histogram with Gnuplot


I want to plot an histogram with Gnuplot and I have the following code:

n=100 #number of intervals
max=31 #max value
min=22  #min value
width=(max-min)/n #interval width
#function used to map a value to the intervals
hist(x,width)=width*floor(x/width)+width/2.0

GNUTERM = "x11"
set terminal postscript enhanced color
set output "histo.ps"
plot "file_log" u (hist($1,width)):(1.0) smooth freq w boxes lc rgb "blue" notitle

Usually this code was working fine, but right now I'm getting a

"histo.gnu", line 48: warning: Skipping data file with no valid points

plot "file_log" u (hist($1,width)):(1.0) smooth freq w boxes lc rgb "blue" notitle 
                                                                                       ^
"histo.gnu", line 48: x range is invalid

I really don't understand why, because the values in the file are indeed between 22 and 31...Any idea?


Solution

  • The error is this line

    width=(max-min)/n #interval width
    

    Since max,min, and n are all integers gnuplot uses integer arithmetic, so 11/100 = 0. You could fix it by saying width=real(max-min)/n or else make sure the limiting values are real numbers:

    n=100 #number of intervals
    max=31.0 #max value
    min=22.0  #min value
    width=(max-min)/n #interval width