Csv gnuplot中的直方图与unix实用程序中的直方图

Csv gnuplot中的直方图与unix实用程序中的直方图,csv,gnuplot,histogram,Csv,Gnuplot,Histogram,我有一个文件,我想从第6列创建直方图。使用Linux实用程序很简单: └──> cut -f6 -d, data.csv | sort | uniq -c | sort -k2,2n 563 0.0 72 0.025 35 0.05 22 0.075 14 0.1 21 0.125 14 0.15 10 0.175 5 0.2 3 0.225 7 0.25 3 0.

我有一个文件,我想从第6列创建直方图。使用Linux实用程序很简单:

└──> cut -f6 -d, data.csv | sort | uniq -c | sort -k2,2n
    563 0.0
     72 0.025
     35 0.05
     22 0.075
     14 0.1
     21 0.125
     14 0.15
     10 0.175
      5 0.2
      3 0.225
      7 0.25
      3 0.275
      6 0.3
      5 0.325
      3 0.35
      1 0.375
      3 0.4
      1 0.425
      3 0.45
      3 0.475
      5 0.5
      7 0.525
     11 0.55
      3 0.575
      4 0.6
      3 0.625
     11 0.65
      5 0.675
      9 0.7
      5 0.725
      7 0.75
      8 0.775
      5 0.8
      3 0.825
      3 0.85
      4 0.875
      2 0.9
      1 0.925
      1 0.975
    109 1.0
但我想使用
gnuplot
绘制它,我的尝试是修改我找到的脚本。这是我的修改版本:

#!/usr/bin/gnuplot -p
# http://psy.swansea.ac.uk/staff/carter/gnuplot/gnuplot_frequency.htm

clear
reset

set datafile separator ",";
# set term dumb

set key off
set border 3

# Add a vertical dotted line at x=0 to show centre (mean) of distribution.
set yzeroaxis

# Each bar is half the (visual) width of its x-range.
set boxwidth 0.05 absolute
set style fill solid 1.0 noborder

bin_width = 0.1;
bin_number(x) = floor(x/bin_width)
rounded(x) = bin_width * ( bin_number(x) + 0.5 )

# MAKE BINS
# plot dataset_path using (rounded($6)):(6) smooth frequency with boxes

# DO NOT MAKE BINS
plot "data.csv" using 6:6 smooth frequency with boxes
结果是:


它所说的与Unix工具完全不同。在
gnuplot
中,我看到了各种类型的柱状图,例如,一些柱状图遵循正态分布模式,其他柱状图根据频率排序(好像我用
sort-n
替换了最后一个
sort-k2,2n
),另一个柱状图根据创建柱状图的数字排序(我的情况),等等。如果我能选择的话,那就太好了

平滑频率
使数据在x中单调(即使用列在第一列中给出的值,在您的情况下是第6列中的数值),然后将所有y值相加(使用列在第二列中给出的值)

这里您也给出了第六列的数值,这是错误的。如果您想计算第六列中每个不同值的出现次数,请使用
使用6:(1)
,即第二列中的数值
1
,来计算每个值的实际出现次数:

set style fill solid noborder
set boxwidth 0.8 relative
set datafile separator ','
plot 'nupic_out.csv' using 6:(1) smooth frequency with boxes notitle

要对平滑后的数据应用对数刻度,必须首先将它们保存到带有
set table。。。;打印
,然后打印此临时文件

set datafile separator ','
set table 'tmp.dat'
plot 'nupic_out.csv' using 6:(1) smooth frequency with lines
unset table
这里您必须注意,因为gnuplot中的一个bug在输出文件中添加了一个错误的最后一行,您必须跳过它。您可以使用
语句中的过滤器跳过此操作,例如

plot 'tmp.dat' using (strcol(3) eq "i" ? $1 : 1/0):2 with boxes
这在这里很好,或者您可以使用
head
来剪切最后两行,如下所示

plot '< head -n-2 tmp.dat' using 1:2 with boxes

现在,对第六列中的值使用binning函数,您必须将
中的
6
替换为使用6:(1)
的函数,该函数对第六列中给出的值进行操作。此函数必须包含在()中,并且在函数中使用
$6
引用第六列中的当前值,如

plot 'nupic_out.csv' using (bin($6)):(1) smooth frequency with lines
同样,一个完整的工作脚本,使用

设置样式填充顺序
设置数据文件分隔符“,”
将boxwidth设置为0.09绝对值
最小值=-0.05
最大值=1.05
n=11.0
宽度=(最大-最小)/n
箱子(x)=宽度*(地板((x-Min)/宽度)+0.5)+Min
设置表格“tmp.dat”
使用(bin($6))绘制“nupic_out.csv”:(1)平滑频率和线条notitle
未设置的表
设置数据文件分隔符空白
设置对数刻度y
设置X范围[-0.05:1.05]
设置错误
使用1:2的比例绘制“

谢谢你,伙计,这很有效。只有几个问题:1。我可以在y轴上使用对数刻度吗?我确实
设置了logscale y 10
,但输出混乱(还有其他数字)。2.我是否可以以某种方式重新排列x轴(如我在OP中所述)。3.将数字封装在()中的目的是什么?我在这里用()尝试了不同的版本
seq 1 12 | xargs-n2 echo | gnuplot-p-e“设置样式填充顺序;设置boxwidth 0.3相对;使用2:1平滑频率与box notitle“
绘制“-”,但我无法理解。谢谢1:不,您不能直接在平滑数据上使用日志比例,我将扩展我的答案,以包括一个可能的解决方案。2.当然可以,您可以在
using
语句中使用任何您想要的表达式,而不是使用
6
,例如
using(四舍五入($6)):(1)
或类似的语句。但是请注意binning函数的细节,请参阅。3. <代码>使用6:1将使用第六列表示x,第一列表示y值。将某些内容包含在()中使gnuplot对表达式求值,
(1)
给出的是数字1,而不是1中的值。上校
plot 'nupic_out.csv' using (bin($6)):(1) smooth frequency with lines
set style fill solid noborder
set datafile separator ','

set boxwidth 0.09 absolute
Min = -0.05
Max = 1.05
n = 11.0
width = (Max-Min)/n
bin(x) = width*(floor((x-Min)/width)+0.5) + Min

set table 'tmp.dat'
plot 'nupic_out.csv' using (bin($6)):(1) smooth frequency with lines notitle
unset table

set datafile separator whitespace
set logscale y
set xrange [-0.05:1.05]
set tics nomirror out
plot '< head -n-2 tmp.dat' using 1:2 with boxes notitle