Gnuplot 没有官方的方法来绘制这种水平箱线图。但是,您可以使用boxxyerrorbars(简写boxxy)来实现这一点。
由于我没有您的实际示例的任何测试数据,因此我从高斯随机游走生成了一个数据文件。要生成数据,请运行以下python 脚本:
from numpy import zeros, savetxt, random
N = 500
g = zeros(N)
for i in range(1, N):
g[i] = g[i-1] + random.normal()
savetxt('randomwalk.dat', g, delimiter='\t', fmt='%.3f')
接下来,我对“位置数据”(在您的情况下是体积数据)进行分箱。对于这个可以使用smooth frequency。这将计算相同 x 值的 y 值的总和。所以首先我使用了一个合适的分箱函数,它在一定范围内返回相同的值(x +- binwidth/2)。输出数据保存在一个文件中,因为要进行绘图,我们必须交换 x 和 y 值:
binwidth = 2
hist(x) = floor(x+0.5)/binwidth
set output "| head -n -2 > randomwalk.hist"
set table
plot 'randomwalk.dat' using (hist($1)):(1) smooth frequency
unset table
unset output
通常应该能够使用set table "randomwalk.hist",但由于一个错误,需要这种解决方法来过滤掉表输出的最后一个条目,请参阅我对Why does the 'set table' option in Gnuplot re-write the first entry in the last line?的回答。
现在实际的绘图部分是:
unset key
set x2tics
set xtics nomirror
set xlabel 'time step'
set ylabel 'position value'
set x2label 'frequency'
set style fill solid 1.0 border lt -1
set terminal pngcairo
set output 'randwomwalk.png'
plot 'randomwalk.hist' using ($2/2.0):($1*binwidth):($2/2.0):(binwidth/2.0) with boxxy lc rgb '#00cc00' axes x2y1,\
'randomwalk.dat' with lines lc rgb 'black'
给出结果(4.6.3,当然取决于你的随机数据):
因此,对于您的数据结构,以下脚本应该可以工作:
reset
binwidth = 2
hist(x) = floor(x+0.5)/binwidth
file = 'data.txt'
histfile = 'pricevolume.hist'
set table histfile
plot file using (hist($2)):($3) smooth unique
unset table
# get the number of records to skip the last one
stats histfile using 1 nooutput
unset key
set x2tics
set xtics nomirror
set xlabel 'time'
set ylabel 'price'
set x2label 'volume'
set style fill solid 1.0 border lt -1
plot histfile using ($2/2.0):($1*binwidth):($2/2.0):(binwidth/2.0) every ::::(STATS_records-2) with boxxy lc rgb '#00cc00' axes x2y1,\
file with lines using 1:2 lc rgb 'black'
请注意,这次跳过最后一个 table 条目是通过使用 stats 命令计算所有条目并使用 every 跳过最后一个条目来完成的(是的,STATS_records-2 是正确的,因为点编号从0 开始)。此变体不需要任何外部工具。
我还使用 smooth unique,它计算 的平均值,而不是总和(使用 smooth frequency 完成)。