尝试使用 Gnuplot 绘制直方图时出错 "xrange is invalid"

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

我想用 Gnuplot 绘制直方图,我有以下代码:

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(,width)):(1.0) smooth freq w boxes lc rgb "blue" notitle

通常这段代码工作正常,但现在我得到一个

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

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

我真的不明白为什么,因为文件中的值确实在 22 到 31 之间...知道吗?

错误是这一行

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

由于 max、min 和 n 都是整数 gnuplot 使用整数运算,所以 11/100 = 0。你可以通过说 width=real(max-min)/n 来修复它,或者确保限制值是实数:

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