我正在尝试在 gnuplot 中绘制水平直方图。
这是我当前的垂直(通常类型)直方图:
width=0.5
hist(x,width)=width*floor(x/width)+width/2.0
set boxwidth width*0.9
set style fill solid 0.5
plot "a" u (hist($2,width)):(1.0) smooth freq w boxes lc 3 notitle
现在我需要的是完全相同的结果,但顺时针旋转 90 度。
我在下面尝试过,但结果确实不是我所期望的。
width=0.5
hist(x,width)=width*floor(x/width)+width/2.0
set boxwidth width*0.9
set style fill solid 0.5
plot "a" u (1.0):(hist($2,width)) smooth freq w boxes lc 3 notitle
虽然这个问题相当老了并且基本上得到了回答,但让我用插图来更新,仅供记录。
据我所知,在当前的gnuplot(5.4.0)中仍然没有专用的水平直方图样式,可能是因为您可以简单地使用
boxxyerror
(如果您知道如何)来实现它,正如用户@的答案中已经提到的那样pygrac 和@adhip agarwala 以及@Christoph 链接的答案中。
因此,没有必要创建垂直直方图并旋转它,因为绘图样式 boxxyerror
已经存在于 gnuplot 4.0 (2004) 甚至更早的版本中。
自从 gnuplot 5.0.0 (2015) 数据块可用以来,因此不再需要将直方图数据
smooth freq
写入磁盘上的文件中。
脚本:(适用于 gnuplot>=5.0.0)
### horizontal histogram
reset session
# create some random test data
set table $Data
set samples 2000
plot '+' u (invnorm(rand(0))+2):(1) w table, \
'+' u (invnorm(rand(0))-2):(0.5) w table
unset table
binwidth = 0.25
bin(x) = binwidth * floor(x/binwidth)
set table $Histo
plot $Data u (bin($1)):2 smooth freq
unset table
set style fill solid 0.5
plot $Histo u ($2/2):1:($2/2):(binwidth/2.) w boxxy ti "Horizontal Histogram"
### end of script
结果:
虽然 gnuplot 中还没有对水平图的通用支持,但您可以使用 boxxyerrorbars 样式制作相当不错的水平条形图。它有一个 6 列输入(x、y、xlow、xhigh、ylow、yhigh)。您只需要事先自己计算条形的值即可。
我刚刚做了这样的事情($2 指的是“中心”):
使用 'median':'center':'min':'max':($2-0.4):($2+0.4) 和 boxxyerrorbars 绘制 'datafile.csv'
如果您想要沿 y 轴使用类别文本标签,我们就开始(我从数据文件的第一列中获取它们):
使用 'median':'center':'min':'max':($2-0.4):($2+0.4):ytic(1) 和 boxxyerrorbars 绘制 'datafile.csv'
谢谢。这有效。这是完成整个事情的一致方法。 首先以表格格式在另一个文件(例如 datatable.txt)中打印数据文件的常规输出
reset
binwidth=0.015
bin(x,width)=width*floor(x/width) + binwidth/2.0
set table
set output 'datatable.txt'
plot './datafile.txt' using (bin($1,binwidth)):(1.0) smooth freq w p
unset table
set output "horizontalhist.png"
pl './datatable.txt' u 2:1:(0.0):2:(($1)-(binwidth/2.0)):(($1)+(binwidth/2.0)) w boxxyerrorbars
这将为您提供水平直方图。
如果位图输出是唯一关心的问题,那么在使用
convert
渲染直方图后,还可以使用 ImageMagick 套件的 gnuplot
命令。
convert -rotate 90 figure_in.png figure_out.png
在此之前,请使用
gnuplot
指令旋转 rotate
内的所有标签。