gnuplot:对直方图使用对数轴

gnuplot : using a logarithmic axis for a histogram

我有一个要从中创建直方图的数据文件。

数据文件是:

-0.1  0  0  JANE
1  1  1  BILL
2  2  1  BILL
1  3  1  BILL
6  4  0  JANE
35 5  0  JANE
9  6  1  BILL
4  7  1  BILL
24 8  1  BILL
28 9  1  BILL
9  10  0  JANE
16 11  1  BILL
4  12  0  JANE
45 13  1  BILL

我的 gnuplot 脚本是:

file='test.txt'
binwidth=10
bin(x,width)=width*floor(x/width)
set boxwidth 1

plot file using (bin(,binwidth)):(1.0) smooth freq with boxes, \
file using (1+(bin(,binwidth))):(1.0) smooth freq with boxes

我想在 y 的对数刻度上绘制此数据。然而,set logscale y 无法处理一些 0 值(因为一些 bin 是空的)。我收到错误 Warning: empty y range [1:1], adjusting to [0.99:1.01]

根据gnuplot的帮助,"The frequency option makes the data monotonic in x; points with the same x-value are replaced by a single point having the summed y-values."

如何获取由 smooth freq with boxes 计算的总和 y 值的 log10()?

您至少可以做两件事。一种是使用 0 和 1 之间的线性轴,然后使用对数轴,如本 answer 中所述。另一种是先绘制到 table,然后设置对数刻度,忽略零值点。

使用正常的线性轴和您的代码(加上 set yrange [0:11]),您的数据看起来:

现在让我们绘制成 table,然后设置对数刻度,然后忽略零值绘制:

file='test.txt'
binwidth=10
bin(x,width)=width*floor(x/width)

set table "data"

plot file using (bin(,binwidth)):(1.0) smooth freq, \
file using (1+(bin(,binwidth))):(1.0) smooth freq

unset table

set boxwidth 1
set logscale y
set yrange [0.1:11]

plot "data" index 0 using ():( == 0 ? 1/0 : ) with boxes lc 1, \
"data" index 1 using ():( == 0 ? 1/0 : ) with boxes lc 2

set table 有时会在图中生成一些不需要的点,您可以在 x = 0 处看到这些点。要摆脱它们,您可以使用 "< grep -v u data" 而不是 "data"