在gnuplot的直方图中仅在几个条上插入标签

时间:2017-05-15 15:04:04

标签: gnuplot histogram labeling

我正在使用以下代码绘制直方图

set style data histogram
#clustered
#set terminal wxt enhanced persist
set term post eps enhanced "Times-Roman, 14"
set output 'tostack.eps'    #change here
#set boxwidth 0.9 
set grid
set auto y
#set auto x
set auto y
#set ylabel format "{/:Bold}"
#set size 1.35,0.35
#set title ""
set style histogram clustered gap 1 title offset 1,0.25
set ylabel "\nXXX (in %)\n\n" font "Times-Roman ,25"     #change here
set xlabel "\nYYY \n" font "Times-Roman,25"                 #change here
#set style fill solid noborder
set style fill pattern  border -1
set key right
set key spacing 3 font "Times-Roman,20"
set xtics font ", 20"
set ytics font ", 20"

plot for [COL=2:5] 'tostack.dat' using COL:xticlabels(1) title columnheader fs pattern 2

我的数据文件是

AA  BB      CC      DD      EE
100 23.6491500555   6.9743235667    6.5497090218    6.9819639165
200 6.4522741669    14.0817294443   15.1392548608   4.4619875307
300 8.8030456951    8.1386311242    12.5224139497   6.7637627586
400 25.7698157655   7.6673254026    10.0040799765   11.7883595409

在生成的直方图中,如何仅为每组中较大值的条形插入标签。

另外我还要把这些标签加粗。

1 个答案:

答案 0 :(得分:2)

为了找到每个组中的最大值,最有可能需要使用外部处理工具。例如,可以使用如下所示的gawk。我们的想法是跳过标题(条件NR>1)并在每一行中找到最大的列号(假设您的数据文件有4个数据列,这个数字将是2,3,4或5)。现在,在直方图中,各个框的组连续居中于0,1,2等。有4个数据列(并且boxwidth设置为1),每个框的宽度为0.2(两个框之间有4个框)每组的中心+一个空盒子,用于"空间")。为了找到x坐标放置标签的位置,因此有必要将数字2,3,4,5转换为b-0.3,b-0.1,b+0.1,b+0.3,其中b表示基于0的组编号:

set terminal postscript eps enhanced "Times-Roman" 14
set output 'tostack.eps'

set grid

set boxwidth 1.0
set style data histogram
set style histogram clustered gap 1 title offset 1,0.25

set ylabel "XXX (in %)" font "Times-Roman, 25"
set xlabel "YYY" font "Times-Roman, 25"

set style fill pattern border -1
set key right
set key spacing 3 font "Times-Roman, 20"

set xtics font ",20"
set ytics font ",20"

plot \
    for [COL=2:5] 'tostack.dat' using COL:xticlabels(1) title columnheader fs pattern 2, \
    "<gawk 'NR>1{ \
        j=0; \
        for(i=2;i<=NF;i++){ \
            if(i == 2 || $i > m){ \
                m=$i;j=i; \
            } \
        } \
        print (NR-2)+(0.2*j-0.7), m; \
    }' tostack.dat" u 1:2:(sprintf("%.3f", column(2))) t "" w labels offset 0,char 1 font "Times-Bold, 16"

然后产生: enter image description here

编辑: 如果在每列中选择最大值,则只需要稍微修改过滤脚本(计算标签位置背后的想法保持不变):

set terminal postscript eps enhanced "Times-Roman" 14
set output 'tostack.eps'

set grid

set boxwidth 1.0
set style data histogram
set style histogram clustered gap 1 title offset 1,0.25

set ylabel "XXX (in %)" font "Times-Roman, 25"
set xlabel "YYY" font "Times-Roman, 25"

set style fill pattern border -1
set key right
set key spacing 3 font "Times-Roman, 20"

set xtics font ",20"
set ytics font ",20"

plot \
    for [COL=2:5] 'tostack.dat' using COL:xticlabels(1) title columnheader fs pattern 2, \
    "<gawk '\
        NR>1{ \
            for(i=2;i<=NF;i++){ \
                if(NR == 2 || $i > m[i-2]){ \
                    m[i-2]=$i; \
                    p[i-2]=NR-2; \
                } \
            } \
        } \
        END{ \
            for(i=0;i<4;i++){ \
                print p[i] + (0.2*i - 0.3), m[i]; \
            } \
        } \
    ' tostack.dat" u 1:2:(sprintf("%.1f", $2)) t "" w labels offset char 0,char 0.5 font "Times-Bold, 12" rotate by 0

产生: enter image description here