gnuplot,直方图行堆叠列的问题

gnuplot, problem with histogram rowstacked columns

我正在根据数据文件创建行堆叠直方图。 这是我正在使用的脚本:

set title "Heat exchanger surface at the optimal solution"

set termopt enhanced 

set grid

set key top left
#set key off

set xlabel "Working fluid"
set ylabel "A [m^{2}]"

#set xrange[0.0:0.0]
#set yrange[0.0:0.0]

set yrange[0.0:10.0]

set offsets 0.1,0.1,0,0

#set datafile missing '0.000000000000000000e+00'
#set datafile missing '0.000000'

set style data histogram
#set style histogram clustered
set style histogram rowstacked
#set style fill solid border
set style fill solid 1.00 border -1
set boxwidth 0.5

#set xtics ("R124" "R124", "Isobutane" "Isobutane", "R245fa" "R245fa", "Isopentane" "Isopentane", "Cyclopentane" "Cyclopentane")

#set xtics nomirror rotate by -45 scale 0 font ",8"

plot\
    newhistogram "conventional cycle",\
    "parasub_t3_dtpprec_5.txt" using ():xticlabels(2) notitle lc "#E74C3C", '' using () notitle lc rgb "#3498DB",\
    newhistogram "recuperative cycle",\
    "parasub_t3_dtpprec_5.txt" using ():xticlabels(2) title "A_{evap}" lc "#E74C3C", '' using () title "A_{cond}" lc rgb "#3498DB", '' using () title "A_{rec}" lc rgb "#1ABC9C",\

只要有多行数据,脚本就可以正常工作。例如下面的数据文件:

#(1) DTsup [oC] (2) T3 [oC] (3) pr [-]  (4) phigh [Pa]   (5) plow [Pa] (6) nth [-] (7) nthr [-] (8) wnet [J/kg] (9) wp [J/kg] (10) we [J/kg] (11) bwr [-] (12) Toout [K] (13) Tooutr [K] (14) Twout [K] (15) Twoutr [K] (16) mo [kg/s] (17) mor [kg/s] (18) mwf [kg/s] (19) mw [kg/s] (20) mwr [kg/s] (21) qevap [J/kg] (22) qevapr [J/kg] (23) qrec [J/kg] (24) qcond [J/kg] (25) qcondr [J/kg] (26) Aevap [m^2] (27) Aevapr [m^2] (28) Arec [m^2] (29) Acond [m^2] (30) Acondr [m^2] (31)  Qin [W] (32) Qinr [W] (33) mbm [kg/s] (34) mbmr [kg/s] (35) Exde [W] (36) Exdp [W] (37) Exdevap [W] (38) Exdevapr [W] (39) Exdrec [W] (40) Exdcond [W] (41) Exdcondr [W] (42) nex [-] (43) nexr [-] (44) nthchp [-] (45) nthchpr [-] (46) nelchp [-] (47) nelchpr [-] (48) nchp [-] (49) nchpr [-] (50) PHR [-] (51) PHRr [-] (52) PESR [-] (53) PESRr [-]
       0.000000      100.00   6.615031   722136.079363   109165.948144    0.120395     0.133603    54105.033173   1255.690368   55360.723541     0.022682     323.399196      342.291477     301.256224      300.341368       0.865955        0.833098        1.026809      11.974715       11.980348     449397.650120      404968.415600     44429.234520     395292.616947      350863.382427         1.749529          1.580108        0.905206         4.741577          4.431979 485732.339365 437710.913321        0.030358         0.027357  12646.313088    249.135367     75334.789746      68040.736716     1051.434109     10361.313998       7598.925672     0.797025      0.795329        0.835625         0.823077        0.102937         0.114231      0.938563       0.937308     0.123186      0.138785      0.193811       0.206780
       0.000000      125.00  10.950918  1195467.347000   109165.948144    0.143171     0.165337    69590.450251   2224.353135   71814.803386     0.030973     323.636526      351.135647     301.686140      300.350555       0.728765        0.695743        0.798322       9.315172        9.314445     486065.769850      420901.502320     65164.267530     416475.319599      351311.052069         1.717772          1.493149        0.995391         3.798056          3.490943 408459.763072 353699.722503        0.025529         0.022106  12367.695114    342.877062     51440.216065      43942.601168     1342.638004      9868.981536       5917.282287     0.818783      0.819301        0.813988         0.792930        0.122411         0.141363      0.936399       0.934293     0.150384      0.178279      0.215916       0.236295
       0.000000      150.00  17.111919  1868038.858437   109165.948144    0.159571     0.190207    82665.881533   3599.317677   86265.199210     0.041724     323.971907      358.988006     302.090891      300.366300       0.654590        0.623571        0.672049       7.826862        7.838158     518050.203910      434609.996396     83440.207514     435384.322377      351944.114863         1.701833          1.434744        1.044041         3.277773          2.973168 366479.289887 307451.983717        0.022905         0.019216  12183.225690    466.601844     38046.705189      30728.594794     1564.632186      9976.733938       4991.692881     0.834443      0.837585        0.798407         0.769303        0.136433         0.162627      0.934841       0.931930     0.170882      0.211395      0.231097       0.257935
       0.000000      175.00  25.632899  2798239.736148   109165.948144    0.170657     0.205830    91441.090019   5498.220729   96939.310748     0.056718     324.566507      362.694767     302.260397      300.382724       0.613275        0.584904        0.607556       7.087607        7.087352     535819.088490      444255.015085     91564.073405     444377.998471      352813.925066         1.589821          1.335576        1.040693         2.995781          2.711167 342673.623125 284115.439112        0.021417         0.017757  12237.465845    643.490745     30526.883339      23645.251436     1591.296117      9834.879936       4528.051268     0.844626      0.849901        0.787876         0.754461        0.145911         0.175985      0.933788       0.930446     0.185196      0.233259      0.241029       0.270913

给出这张图:

单行数据情况如下:

#(1) DTsup [oC] (2) T3 [oC] (3) pr [-]  (4) phigh [Pa]   (5) plow [Pa] (6) nth [-] (7) nthr [-] (8) wnet [J/kg] (9) wp [J/kg] (10) we [J/kg] (11) bwr [-] (12) Toout [K] (13) Tooutr [K] (14) Twout [K] (15) Twoutr [K] (16) mo [kg/s] (17) mor [kg/s] (18) mwf [kg/s] (19) mw [kg/s] (20) mwr [kg/s] (21) qevap [J/kg] (22) qevapr [J/kg] (23) qrec [J/kg] (24) qcond [J/kg] (25) qcondr [J/kg] (26) Aevap [m^2] (27) Aevapr [m^2] (28) Arec [m^2] (29) Acond [m^2] (30) Acondr [m^2] (31)  Qin [W] (32) Qinr [W] (33) mbm [kg/s] (34) mbmr [kg/s] (35) Exde [W] (36) Exdp [W] (37) Exdevap [W] (38) Exdevapr [W] (39) Exdrec [W] (40) Exdcond [W] (41) Exdcondr [W] (42) nex [-] (43) nexr [-] (44) nthchp [-] (45) nthchpr [-] (46) nelchp [-] (47) nelchpr [-] (48) nchp [-] (49) nchpr [-] (50) PHR [-] (51) PHRr [-] (52) PESR [-] (53) PESRr [-]
       0.000000      100.00   5.341869  2378728.179011   445298.822341    0.112752     0.114041    19161.460322   1801.363568   20962.823891     0.085931     324.451846      326.146202     300.506605      300.412249       0.927877        0.922580        2.899338      14.211155       14.212341     169943.894226      168023.173176      1920.721050     150782.433904      148861.712854         1.530831          1.516876        0.148261         5.350023          5.288725 518657.725517 512795.809645        0.032416         0.032050  14432.854403   1006.224611     85187.593357      84077.690879       91.521847      9284.760286       9108.082145     0.788085      0.787993        0.842886         0.841661        0.096403         0.097505      0.939289       0.939166     0.114372      0.115848      0.186111       0.187420

两列合并为一列,如下图所示:

老实说,我不知道该如何处理。

此外,我想知道是否有一种方法可以使框宽度保持不变,而不管列数如何。

提前致谢。

编辑:感谢 theozh 和 ethan 优雅的解决方案,问题已解决。这种行为似乎是一个错误,因为原始脚本在单行数据文件的情况下特别失败。

这种行为在我看来像是一个错误(或者是我不了解直方图样式的工作原理)。 通常,您可以通过 every ::a::b(检查 help every)将您的数据限制为仅原始数据的一部分。 在您的情况下,只要 b>a,它就会按预期工作。 但是,当您的数据仅包含一行时,即您将其限制为一行,例如a=0b=0 你会得到奇怪的结果。 (到目前为止)我可以分开条形的唯一方法是引入一个虚拟的新直方图。 这会造成很大的差距,这不是很好。所以,我希望有更好的解决方案。

关于boxwidth,你可以设置为绝对值或相对值。检查 help boxwidth.

脚本:

### histogram rowstacked with just one row
reset session

$Data <<EOD
#(1) DTsup [oC] (2) T3 [oC] (3) pr [-]  (4) phigh [Pa]   (5) plow [Pa] (6) nth [-] (7) nthr [-] (8) wnet [J/kg] (9) wp [J/kg] (10) we [J/kg] (11) bwr [-] (12) Toout [K] (13) Tooutr [K] (14) Twout [K] (15) Twoutr [K] (16) mo [kg/s] (17) mor [kg/s] (18) mwf [kg/s] (19) mw [kg/s] (20) mwr [kg/s] (21) qevap [J/kg] (22) qevapr [J/kg] (23) qrec [J/kg] (24) qcond [J/kg] (25) qcondr [J/kg] (26) Aevap [m^2] (27) Aevapr [m^2] (28) Arec [m^2] (29) Acond [m^2] (30) Acondr [m^2] (31)  Qin [W] (32) Qinr [W] (33) mbm [kg/s] (34) mbmr [kg/s] (35) Exde [W] (36) Exdp [W] (37) Exdevap [W] (38) Exdevapr [W] (39) Exdrec [W] (40) Exdcond [W] (41) Exdcondr [W] (42) nex [-] (43) nexr [-] (44) nthchp [-] (45) nthchpr [-] (46) nelchp [-] (47) nelchpr [-] (48) nchp [-] (49) nchpr [-] (50) PHR [-] (51) PHRr [-] (52) PESR [-] (53) PESRr [-]
       0.000000      100.00   6.615031   722136.079363   109165.948144    0.120395     0.133603    54105.033173   1255.690368   55360.723541     0.022682     323.399196      342.291477     301.256224      300.341368       0.865955        0.833098        1.026809      11.974715       11.980348     449397.650120      404968.415600     44429.234520     395292.616947      350863.382427         1.749529          1.580108        0.905206         4.741577          4.431979 485732.339365 437710.913321        0.030358         0.027357  12646.313088    249.135367     75334.789746      68040.736716     1051.434109     10361.313998       7598.925672     0.797025      0.795329        0.835625         0.823077        0.102937         0.114231      0.938563       0.937308     0.123186      0.138785      0.193811       0.206780
       0.000000      125.00  10.950918  1195467.347000   109165.948144    0.143171     0.165337    69590.450251   2224.353135   71814.803386     0.030973     323.636526      351.135647     301.686140      300.350555       0.728765        0.695743        0.798322       9.315172        9.314445     486065.769850      420901.502320     65164.267530     416475.319599      351311.052069         1.717772          1.493149        0.995391         3.798056          3.490943 408459.763072 353699.722503        0.025529         0.022106  12367.695114    342.877062     51440.216065      43942.601168     1342.638004      9868.981536       5917.282287     0.818783      0.819301        0.813988         0.792930        0.122411         0.141363      0.936399       0.934293     0.150384      0.178279      0.215916       0.236295
       0.000000      150.00  17.111919  1868038.858437   109165.948144    0.159571     0.190207    82665.881533   3599.317677   86265.199210     0.041724     323.971907      358.988006     302.090891      300.366300       0.654590        0.623571        0.672049       7.826862        7.838158     518050.203910      434609.996396     83440.207514     435384.322377      351944.114863         1.701833          1.434744        1.044041         3.277773          2.973168 366479.289887 307451.983717        0.022905         0.019216  12183.225690    466.601844     38046.705189      30728.594794     1564.632186      9976.733938       4991.692881     0.834443      0.837585        0.798407         0.769303        0.136433         0.162627      0.934841       0.931930     0.170882      0.211395      0.231097       0.257935
       0.000000      175.00  25.632899  2798239.736148   109165.948144    0.170657     0.205830    91441.090019   5498.220729   96939.310748     0.056718     324.566507      362.694767     302.260397      300.382724       0.613275        0.584904        0.607556       7.087607        7.087352     535819.088490      444255.015085     91564.073405     444377.998471      352813.925066         1.589821          1.335576        1.040693         2.995781          2.711167 342673.623125 284115.439112        0.021417         0.017757  12237.465845    643.490745     30526.883339      23645.251436     1591.296117      9834.879936       4528.051268     0.844626      0.849901        0.787876         0.754461        0.145911         0.175985      0.933788       0.930446     0.185196      0.233259      0.241029       0.270913
EOD

set title "Heat exchanger surface at the optimal solution"
set termopt enhanced 
set grid

set xlabel "Working fluid"
set ylabel "A [m^{2}]"
set yrange[0.0:10.0]
set offsets 0.1,0.1,0,0
set key top left noautotitle
set style data histogram
set style histogram rowstacked
set style fill solid 1.00 border -1
set boxwidth 0.5

a=0
b=0
plot\
    newhistogram "conventional cycle", \
    $Data u ():xtic(2) every ::a::b lc rgb "#E74C3C" , \
       '' u ()         every ::a::b lc rgb "#3498DB", \
    newhistogram "", '' u (0), \
    newhistogram "recuperative cycle", \
    '' u ():xtic(2) every ::a::b title "A_{evap}" lc rgb "#E74C3C", \
    '' u ()         every ::a::b title "A_{cond}" lc rgb "#3498DB", \
    '' u ()         every ::a::b title "A_{rec}"  lc rgb "#1ABC9C"
### end of script

结果:

Panus Cladus> In the case of single row data like the following, the two columns collapse into one.

theozh> The only way (so far) I could separate the bars is introducing a dummy newhistogram. This creates a large gap which is not so nice. So, I hope there is a much better solution.

newhistogram 绘图元素采用可选关键字 at <x-coordinate>。您可以修改您的 plot 命令,为每个系列的集群堆栈提供起始 x。例如,假设您在一个图中绘制许多这样的数据文件;图中有不同数量的线,但您知道最大值为 10。您可以构建一个 plot 命令

  file(i) = sprintf("File%d.dat",i)
  plot for [i=1:N] \
       newhistogram at i*10 title file(i), \
       file(i) using (), '' using (), ...

一个down-side的解决方案是你可能不希望在短数据文件生成的簇之后额外space。 auto-placement 本来可以避免这种情况,但如前所述,当只有一行数据时它似乎有一个错误。

Work-around:

故障似乎是专门发生的,因为第一个位置是在 x=0。您可以通过为第一个文件 提供明确的起始 x-coordinate 来解决这个问题。 这适用于 1 行数据文件和作为示例给出的 4 行数据文件。

plot\
    newhistogram at 1 "conventional cycle",\
    DATA using ():xticlabels(2) notitle lc "#E74C3C", '' using () notitle lc rgb "#3498DB",\
    newhistogram "recuperative cycle",\
    "" using ():xticlabels(2) title "A_{evap}" lc "#E74C3C", '' using () title "A_{cond}" lc 
rgb "#3498DB", '' using () title "A_{rec}" lc rgb "#1ABC9C",\