5

我的问题是有没有办法在 gnuplot 中制作水平直方图?以下是直方图的组成数据集:

sample  M   N   O   P   Q   R   S   T   Total
A   39.17   8.33    11.07   8.57    22.20   22.93   1.47    1.13    114.87
B   61.68   15.53   16.23   15.55   35.68   36.35   2.32    2.23    185.58
C   30.20   7.35    7.55    7.48    16.90   17.50   1.18    1.10    89.25
D   12.57   5.18    4.98    5.77    9.88    8.18    0.67    0.83    48.07
E   36.80   9.00    11.07   9.43    21.43   22.63   0.93    1.03    112.33
F   62.05   15.57   15.95   15.70   35.68   36.63   2.27    2.47    186.32
G   30.23   7.33    7.55    7.48    17.10   17.68   1.10    1.25    89.70
H   12.97   4.87    5.22    5.18    10.37   7.90    0.98    0.70    48.18
I   17.67   12.80   9.40    5.67    32.47   18.53   1.13    0.63    98.30
J   72.30   13.70   17.43   15.43   30.62   38.43   2.25    2.43    192.60
K   25.70   7.58    12.45   4.60    19.30   18.88   1.10    0.80    90.40
L   16.15   3.80    3.53    10.43   7.27    8.80    0.88    1.10    51.97

我从这里修改了代码以绘制直方图:

    set terminal png font "Times-Roman,9" 
    set output 'sample.png'
    set border 3 front linetype -1 linewidth 1.000
    set boxwidth 0.85 absolute
    set style fill   solid 1.00 border lt -1
    set grid nopolar
    set grid noxtics nomxtics ytics nomytics noztics nomztics \
    nox2tics nomx2tics noy2tics nomy2tics nocbtics nomcbtics
    set grid layerdefault   linetype 0 linewidth 1.000,  linetype 0 linewidth 1.000
    set key outside right top vertical Left reverse noenhanced autotitles columnhead nobox
    set key invert samplen 4 spacing 1 width 0 height 0 
    set style histogram rowstacked title  offset character 0, 0, 0
    set datafile missing '-'
    set style data histograms
    #set style histogram gap 1
    set xtics border in scale 0,0 nomirror rotate by -45  offset character 0, 0, 0 autojustify
    set xtics  norangelimit font ",8"
    set xtics   ()
    #set noytics
    set ylabel "% of total" 
    set yrange [ 0 : 100 ] #noreverse nowriteback
    plot 'sample.txt' using (100.*$2/$10):xtic(1) t column(2), for [i=3:9] '' using (100.*column(i)/column(10)) title column(i)

这给了我:

直方图

gnuplot 有没有办法让它横着走?

4

1 回答 1

1

这是一个很老的问题,但仍然没有答案。正如@Christoph 建议的那样,绘图风格boxxyerror是解决方法。但是,到目前为止,我还没有看到示例代码,例如在 gnuplot 演示页面上。据我所知,gnuplot 5.2.8 和可能的 5.4 仍然没有绘图风格的水平堆叠直方图(如果我错了,请纠正我)。

实际上,“总计”列不是必需的,因为 gnuplot 可以自动计算总和。

代码:(gnuplot >=5.2)

### horizontal stacked histogram
reset session

$Data <<EOD
sample  M   N   O   P   Q   R   S   T   Total
A   39.17   8.33    11.07   8.57    22.20   22.93   1.47    1.13    114.87
B   61.68   15.53   16.23   15.55   35.68   36.35   2.32    2.23    185.58
C   30.20   7.35    7.55    7.48    16.90   17.50   1.18    1.10    89.25
D   12.57   5.18    4.98    5.77    9.88    8.18    0.67    0.83    48.07
E   36.80   9.00    11.07   9.43    21.43   22.63   0.93    1.03    112.33
F   62.05   15.57   15.95   15.70   35.68   36.63   2.27    2.47    186.32
G   30.23   7.33    7.55    7.48    17.10   17.68   1.10    1.25    89.70
H   12.97   4.87    5.22    5.18    10.37   7.90    0.98    0.70    48.18
I   17.67   12.80   9.40    5.67    32.47   18.53   1.13    0.63    98.30
J   72.30   13.70   17.43   15.43   30.62   38.43   2.25    2.43    192.60
K   25.70   7.58    12.45   4.60    19.30   18.88   1.10    0.80    90.40
L   16.15   3.80    3.53    10.43   7.27    8.80    0.88    1.10    51.97
EOD

set xlabel "% of total"
set xrange [0:103]
set yrange [:] reverse
set offsets 0,0,0.5,0.5
set style fill solid 1.0
set key out

ColCount = 8
myBoxwidth = 0.8

plot for [col=2:ColCount+1] $Data u col:0: \
     (total=(sum [i=2:ColCount+1] column(i)),(sum [i=2:col-1] column(i)/total*100)): \
     ((sum [i=2:col] column(i))/total*100): \
     ($0-myBoxwidth/2.):($0+myBoxwidth/2.):ytic(1) w boxxyerror ti columnhead(col)
### end of code

结果:

在此处输入图像描述

代码:(gnuplot 4.6的版本,OP的问题时间)。结果与上面类似,文件中的数据Data.dat

### horizontal stacked histogram, version for gnuplot 4.6
reset

FILE = "Data.dat"

set xlabel "% of total"
set xrange [0:103]
set yrange [:] reverse
set offsets 0,0,0.5,0.5
set style fill solid 1.0
set key out

ColCount = 8
myBoxwidth = 0.8

plot for [col=2:ColCount+1] FILE u col:0: \
     (total=(sum [i=2:ColCount+1] column(i)),(sum [i=2:col-1] column(i)/total*100)): \
     ((sum [i=2:col] column(i))/total*100): \
     ($0-myBoxwidth/2.):($0+myBoxwidth/2.):ytic(1) w boxxyerror ti columnhead(col)
### end of code
于 2020-07-16T22:41:04.417 回答