Search code examples
gnuplothistogram

gnuplot, problem with histogram rowstacked columns


I'm creating rowstacked histograms from data files. Here is the script i'm using:

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 ($26):xticlabels(2) notitle lc "#E74C3C", '' using ($29) notitle lc rgb "#3498DB",\
    newhistogram "recuperative cycle",\
    "parasub_t3_dtpprec_5.txt" using ($27):xticlabels(2) title "A_{evap}" lc "#E74C3C", '' using ($30) title "A_{cond}" lc rgb "#3498DB", '' using ($28) title "A_{rec}" lc rgb "#1ABC9C",\

As long as there are multiple lines of data, the script is working as intended. For instance, the following data file:

#(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

gives this graph: picture_01

In the case of single row data like the following:

#(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

the two columns collapse into one, like the next plot suggests: picture_02

I honestly don't know how to handle this.

In addition, i was wondering if there is a way to keep box width constant, regardless of the number of columns.

Thank you in advance.

Edit: Thanks to theozh and ethan's elegant work-around solutions, the issue is resolved. It would seem that this behavior is a bug, since the original script fails specifically in cases of single-row data files.


Solution

  • This behaviour looks to me like a bug (or it is my lack of understanding how histogram style works). In general, you can limit your data to only a part of the original data via every ::a::b (check help every). In your case, as long as b>a, it will work as expected. However, when your data consists only of one row, i.e. you limit it to one row, e.g. a=0 and b=0 you will get the strange result you got. 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.

    About the boxwidth, you can set it to absolute or relative values. Check help boxwidth.

    Script:

    ### 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 ($26):xtic(2) every ::a::b lc rgb "#E74C3C" , \
           '' u ($29)         every ::a::b lc rgb "#3498DB", \
        newhistogram "", '' u (0), \
        newhistogram "recuperative cycle", \
        '' u ($27):xtic(2) every ::a::b title "A_{evap}" lc rgb "#E74C3C", \
        '' u ($30)         every ::a::b title "A_{cond}" lc rgb "#3498DB", \
        '' u ($28)         every ::a::b title "A_{rec}"  lc rgb "#1ABC9C"
    ### end of script
    

    Result:

    enter image description here