【问题标题】:Histogram with numeric x-axis in gnuplot?gnuplot中带有数字x轴的直方图?
【发布时间】:2011-03-26 17:14:37
【问题描述】:

我将这个文件作为data.dat:

Xstep Y1 Y2 Y3 Y4
332 1.22 0.00 0.00 1.43
336 5.95 12.03 6.11 10.41
340 81.05 81.82 81.92 81.05
394 11.76 6.16 10.46 5.87
398 0.00 0.00 1.51 1.25
1036 0.03 0.00 0.00 0.00

我可以使用此脚本hist-v1.gplot(使用set style data histogram)将此数据绘制为直方图:

set xlabel "X values"
set ylabel "Occurence"
set style data histogram
set style histogram cluster gap 1
set style fill solid border -1
set term png
set output 'hist-v1.png'
set boxwidth 0.9
# attempt to set xtics so they are positioned numerically on x axis:
set xtics ("332" 332, "336" 336, "340" 340, "394" 394, "398" 398, "1036" 1036)  
# ti col reads the first entry of the column, uses it as title name 
plot 'data.dat' using 2:xtic(1) ti col,  '' u 3 ti col, '' u 4 ti col, '' u 5 ti col

然后通过调用:

gnuplot hist-v1.gplot && eog hist-v1.png

生成此图像:

但是,您会注意到 X 轴没有按数字缩放 - 它将 X 值理解为类别(即它是一个类别轴)。

我可以使用以下脚本hist-v2.gplot(使用with boxes)获得更多数字X轴:

set xlabel "X values"
set ylabel "Occurence"
# in this case, histogram commands have no effect
set style data histogram
set style histogram cluster gap 1
set style fill solid border -1
set term png
set output 'hist-v2.png'
set boxwidth 0.9
set xr [330:400]
# here, setting xtics makes them positioned numerically on x axis:
set xtics ("332" 332, "336" 336, "340" 340, "394" 394, "398" 398, "1036" 1036)  
# 1:2 will ONLY work with proper xr; since we have x>300; xr[0:10] generates "points y value undefined"!
plot 'data.dat' using 1:2 ti col smooth frequency with boxes, '' u 1:3 ti col smooth frequency with boxes

然后通过调用:

gnuplot hist-v2.gplot && eog hist-v2.png

生成此图像: image hist-v2.png http://img266.imageshack.us/img266/6717/histv2.png

不幸的是,这里的条形“重叠”,因此很难阅读图表。

有没有办法像hist-v2.png 那样保持数字刻度 X 轴,但像 hist-v1.png 那样保持“条”并排?这个线程,“Re: Histogram with x axis date error”说你不能:

但是很难从数据文件中提取 x 坐标日期,...

但是,它指的是另一个问题......

谢谢,

干杯!

【问题讨论】:

    标签: gnuplot histogram numeric


    【解决方案1】:

    好的,在阅读了 gnuplot 帮助之后,似乎直方图样式将“始终”将 x 轴解释为连续条目/类别 - 所以实际上,似乎没有办法获得数字轴带有直方图样式。

    然而,事实证明$ 可以引用一个列,并且这些可以用于在第二个(frequency with boxes 样式)示例中实际“重新定位”条形;所以这个代码为hist-v2b.gplot

    set xlabel "X values"
    set ylabel "Occurence"
    set style fill solid border -1
    set term png
    set output 'hist-v2.png'
    set boxwidth 0.9
    set xr [330:400]
    # here, setting xtics makes them positioned numerically on x axis:
    set xtics ("332" 332, "336" 336, "340" 340, "394" 394, "398" 398, "1036" 1036)  
    # 1:2 will ONLY work with proper xr; since we have x>300; xr[0:10] generates "points y value undefined"!
    plot 'data.dat' using ($1-0.5):2 ti col smooth frequency with boxes, '' u ($1-0.25):3 ti col smooth frequency with boxes, '' u ($1+0.25):4 ti col smooth frequency with boxes, '' u ($1+0.5):5 ti col smooth frequency with boxes
    

    然后通过调用:

    gnuplot hist-v2b.gplot && eog hist-v2b.png
    

    生成此图像: image hist-v2b.png http://img823.imageshack.us/img823/805/histv2b.png

    ...这正是我最初想要的。

    只是一个小提示-我最初想将脚本与内联数据一起使用;对于这样的设置,它必须写成

    plot '-' using ($1-0.5):2 ti col smooth frequency with boxes, '-' u ($1-0.25):3 ti col smooth frequency with boxes
    Xstep Y1 Y2 Y3 Y4
    332 1.22 0.00 0.00 1.43
    336 5.95 12.03 6.11 10.41
    340 81.05 81.82 81.92 81.05
    394 11.76 6.16 10.46 5.87
    398 0.00 0.00 1.51 1.25
    1036 0.03 0.00 0.00 0.00
    end
    Xstep Y1 Y2 Y3 Y4
    332 1.22 0.00 0.00 1.43
    336 5.95 12.03 6.11 10.41
    340 81.05 81.82 81.92 81.05
    394 11.76 6.16 10.46 5.87
    398 0.00 0.00 1.51 1.25
    1036 0.03 0.00 0.00 0.00
    end
    

    ...也就是说,数据必须多次输入,因为它来自标准输入 - 这个问题在 gnuplot - do multiple plots from data file with built-in commands 中讨论。

    干杯!

    PS:由于图表上有相当多的空间,如果我们能以某种方式指定单独的 x 轴范围,那就太好了;讨论在:

    【讨论】:

      【解决方案2】:

      当您使用“盒子”绘图样式绘制直方图时,正确设置盒子宽度非常重要。在我的一篇博客文章中,我谈到了它。有兴趣可以点击here

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-10-05
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多